Differences between ExpandoObject, DynamicObject and dynamic

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.

So, if you need to handle a dynamic type: use dynamic and if you need to handle dynamic data such as XML or JSON: use ExpandoObject

The declaration of an expando shows the relationship between dynamic and the expando:

dynamic expando = new ExpandoObject();

And the ability to add a new property:

expando.SomeNewStringVal = "Hello World!";

That last line of code creates a brand new string property in the expando object calledSomeNewStringVal. The string type is inferred from the assignment.

So an expando is a dynamic data type that can represent dynamically changing data. That’s it in a nutshell.

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 IDynamicMetaObjectProviderimplementation, the DLR calls the IDynamicMetaObjectProvider methods and the object itself decides what to do.

The ExpandoObject and DynamicObject classes are implementations ofIDynamicMetaObjectProvider.

ExpandoObject is a simple class which allows you to add members to an instance and use themdynamically.
DynamicObject is a more advanced implementation which can be inherited to easily provide customized behavior.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s