The following code seems reasonable to me. It should create the object and then use the dynamic features to let me assign any properties I like. However the compiler says that "ExpandoObject does not contain a definition for Test". To which I say, "I know, that's the freaking point!"
dynamic example = new ExpandoObject
{
Test = "fail"
};
Any ideas why csc isn't allowing this.
The alternative is to manually expand the code into individual property assignments.
dynamic example = new ExpandoObject();
example.Test = "fail";
Which is annoying when I have lots of properties to assign.
Within the object initializer, the type is ExpandoObject
, not dynamic
, so you don't get dynamic functionality. After the initializer, you are operating on a variable of type dynamic
and so dynamic functionality is available there.
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