What are the differences between System.Dynamic.ExpandoObject
, System.Dynamic.DynamicObject
and dynamic
?
In which situations do you use these types?
What are the differences between System.Dynamic.ExpandoObject
, System.Dynamic.DynamicObject
and dynamic
?
In which situations do you use these types?
dynamic
is a type declaration. I.e. dynamic x
means the variable x
has the type dynamic
.
DynamicObject
is a type that makes it easy to implement IDynamicMetaObjectProvider
and thus override specific binding behavior for the type.
ExpandoObject
is a type that acts like a property bag. I.e. you can add properties, methods and so forth to dynamic instances of this type at runtime.
The dynamic
keyword is used to declare variables that should be late-bound.
If you want to use late binding, for any real or imagined type, you use the dynamic
keyword and the compiler does the rest.
When you use the dynamic
keyword to interact with a normal instance, the DLR performs late-bound calls to the instance's normal methods.
The IDynamicMetaObjectProvider
interface allows a class to take control of its late-bound behavior.
When you use the dynamic
keyword to interact with an IDynamicMetaObjectProvider
implementation, the DLR calls the IDynamicMetaObjectProvider
methods and the object itself decides what to do.
The ExpandoObject
and DynamicObject
classes are implementations of IDynamicMetaObjectProvider
.
ExpandoObject
is a simple class which allows you to add members to an instance and use them dynamic
ally.
DynamicObject
is a more advanced implementation which can be inherited to easily provide customized behavior.