I would like to dynamically add properties to a ExpandoObject at runtime. So for example to add a string property call NewProp I would like to write something like
var x = new ExpandoObject(); x.AddProperty("NewProp", System.String);
Is this easily possible?
The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members. This class supports dynamic binding, which enables you to use standard syntax like sampleObject. sampleMember instead of more complex syntax like sampleObject.
Dynamic objects expose members such as properties and methods at run time, instead of at compile time. This enables you to create objects to work with structures that do not match a static type or format.
1.2 Basic Dynamic Properties and Their Significance Theoretically, it can be defined as the ratio of stress to strain resulting from an oscillatory load applied under tensile, shear, or compression mode.
You can use this to see if a member is defined: var expandoObject = ...; if(((IDictionary<String, object>)expandoObject). ContainsKey("SomeMember")) { // expandoObject. SomeMember exists. }
dynamic x = new ExpandoObject(); x.NewProp = string.Empty;
Alternatively:
var x = new ExpandoObject() as IDictionary<string, Object>; x.Add("NewProp", string.Empty);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With