Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding properties to a dynamic object?

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 ?

like image 233
ashutosh raina Avatar asked Dec 03 '11 15:12

ashutosh raina


2 Answers

Here is a cleaner way

var myObject = new ExpandoObject() as IDictionary<string, Object>; myObject.Add("Country", "Ireland");

like image 60
Rustovich Avatar answered Oct 30 '22 02:10

Rustovich


dynamic d = new ExpandoObject();
((IDictionary<string,object>)d)["test"] = 1;
//now you have d.test = 1
like image 22
Cheng Chen Avatar answered Oct 30 '22 03:10

Cheng Chen