What are the differences between System.Dynamic.ExpandoObject
, System.Dynamic.DynamicObject
and dynamic
?
In which situations do you use these types?
cdynamicdynamicobjectexpandoobjectnet
What are the differences between System.Dynamic.ExpandoObject
, System.Dynamic.DynamicObject
and dynamic
?
In which situations do you use these types?
Best Answer
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 anIDynamicMetaObjectProvider
implementation, the DLR calls theIDynamicMetaObjectProvider
methods and the object itself decides what to do.The
ExpandoObject
andDynamicObject
classes are implementations ofIDynamicMetaObjectProvider
.ExpandoObject
is a simple class which allows you to add members to an instance and use themdynamic
ally.DynamicObject
is a more advanced implementation which can be inherited to easily provide customized behavior.