Sunday, December 22, 2013

The C# Dynamic Properties' implementation and a full Expando

It's implemented by the DynamicObject from .NET Framework.
Just an straightforward example in the MSDN - DynamicObject Class.

Wow! But also we have the ExpandoObject.

Some tip from MSDN
The ExpandoObject class is an implementation of the dynamic object concept that enables getting, setting, and invoking members. If you want to define types that have their own dynamic dispatch semantics, use the DynamicObject class. If you want to define how dynamic objects participate in the interoperability protocol and manage DLR fast dynamic dispatch caching, create your own implementation of the IDynamicMetaObjectProvider interface.
dynamic sampleObject = new ExpandoObject();

sampleObject.test = "Dynamic Property";
// Create a new event and initialize it with null.
sampleObject.sampleEvent = null;
// Add an event handler.
sampleObject.sampleEvent += new EventHandler(SampleHandler);

Main features:
  • Dynamic properties and methods
  • Enumerating and Deleting Members
  • !!!
    Receiving Notifications of Property Changes with the INotifyPropertyChanged.
    I.e. it supports the Data Binding with Change Notifications out of the box.
The ExpandoObject class implements the INotifyPropertyChanged interface and can raise a PropertyChanged event when a member is added, deleted, or modified. This enables ExpandoObject class integration with Windows Presentation Foundation (WPF) data binding and other environments that require notification about changes in the object content.

No comments:

Post a Comment