Dynamically adding properties to a dynamic object?

ashutosh raina picture ashutosh raina · Dec 3, 2011 · Viewed 14.5k times · Source

i have this

dynamic d = new ExpandoObject();
d.Name = attribute.QualifiedName.Name;

so , i know that d will have a property Name. Now if i don't know the name of the property at compile time , how do i add that property to the dynamic. i found this SO Question

so, there is this complicated concept of call binders etc..which is tough to get in the first place.any simpler way of doing this ?

Answer

Cheng Chen picture Cheng Chen · Dec 3, 2011
dynamic d = new ExpandoObject();
((IDictionary<string,object>)d)["test"] = 1;
//now you have d.test = 1