I'm looking for a way to add members dynamically to an dynamic object. OK, I guess a little clarification is needed...
When you do that :
dynamic foo = new ExpandoObject();
foo.Bar = 42;
The Bar
property will be added dynamically at runtime. But the code still refers "statically" to Bar (the name "Bar" is hard-coded)... What if I want to add a property at runtime without knowing its name at compile time ?
I know how to do this with a custom dynamic object (I actually blogged about it a few months ago), using the methods of the DynamicObject
class, but how can I do it with any dynamic object ?
I could probably use the IDynamicMetaObjectProvider
interface, but I don't understand how to use it. For instance, what argument should I pass to the GetMetaObject
method ? (it expects an Expression
)
And by the way, how do you perform reflection on dynamic objects ? "Regular" reflection and TypeDescriptor
don't show the dynamic members...
Any insight would be appreciated !
foo.$Bar = 42;
:) Not sure if it's allowed for dynamic... – Roughcastfoo.$Bar
is just shorthand forfoo["Bar"]
– Loftus