ExpandoObject Class
This class represents an object whose members can be dynamically added and removed at run time.
This class implements the following intefaces
IDynamicMetaObjectProvider
IDictionary
ICollection
IEnumerable
IEnumerable
INotifyPropertyChanged
(for details about these interfaces please go theough MSDN documentation)
The ExpandoObject class enables developer to add and delete members of its instances at run time.
This class is also used to set and get values of these members.
It supports dynamic binding, which enables developers to use standard syntax rather than more complex complex syntax that are currently used.
For example:
consider an object _Pobj which has associated member named MyMember
It can be invoked as _Pobj.MyMember rather than _Pobj .GetAttribute("MyMember").
The ExpandoObject class implements the standard Dynamic Language Runtime (DLR) interface IDynamicMetaObjectProvider, which enables to share instances of the ExpandoObject class between languages that support the DLR interoperability model.
In C#, to enable late binding for an instance of the ExpandoObject class, the dynamic keyword is used.
Example:
dynamic myexpandoobject= new ExpandoObject();
//Adding New Members
//properties, methods, and events can be added to instances of the myexpandoobject class.
sampleObject.testdynamicProperty = "MyDynamicProperty";
Here we are adding testdynamicProperty property dynamically to myexpandoobject(object of ExpandoObject Class).
Let's go through another example that shows how to add an event to an instance of the ExpandoObject class.
class MySampleClass
{
MySampleClass()
{
dynamic addeventobject= new ExpandoObject();
// Create a new event and initialize it with null.
addeventobject.EventToBeAdded = null;
// Add an event handler.
addeventobject.EventToBeAdded += new EventHandler(MyEventHandler);
// Raise an event for testing purposes.
addeventobject.EventHandler(addeventobject, new EventArgs());
}
// Event handler.
static void MyEventHandler(object sender, EventArgs e)
{
//Code for handling event
}
}
Let's try to explore some more aspects in which the expando object can be used.
1.Passing ExpandoObject as Parameter
2.Enumerating and Deleting Members
Passing ExpandoObject as Parameter
Like any other ordinary object, expando can be passed as a parameter to the methods.
(not to forget the dynamic keyword)
Example:
class ParameterPassingTest
{
ParameterPassingTest()
{
}
public void CreateObject()
{
dynamic developer, Projectmanager;
developer= new ExpandoObject();
developer.Name = "Pradeep Patel";
developer.Age = 27;
Projectmanager = new ExpandoObject();
Projectmanager.Name = "PM";
Projectmanager.Age = 38;
Projectmanager.TeamSize = 25;
WritePerson(developer);
WritePerson(Projectmanager);
}
private static void WritePerson(dynamic details)
{
Response.Write("{0} is {1} years old.",details.Name, details.Age);
}
}
In the above exaple, if developer object is passed and we try to access the TeamSize property on this object an exception will occur i.e.If we write details.TeamSize , while passing developer object.
It become very obvious that we need to take some extra care while dealing with expando objects(during the initial phases, when we are just learing its usage)
Enumerating and Deleting Members
Enumeration
As mentioned in the beginning, the ExpandoObject class implements the IDictionary
This enables enumeration of members added to the instance of the ExpandoObject class at run time.
Now let's write another example to test enumeration of expando object elements by casting an instance of the ExpandoObject class to the IDictionary
sse.Name = "Pradeep Patel";
sse.Age = 27;
sse.Qualification="B.Tech";
string details=string.Empty;
foreach (var property in (IDictionary
{
details+=property.Key + ": " + property.Value)+"";
}
(Please not the code is only for illustration purpose and has not been optimized ofr checked)
Deletion
C# does not have any sntax for deleting members hence developers need to cast an instance of the ExpandoObject to the IDictionary
Following code snippet shows the same
dynamic sse = new ExpandoObject();
employee.Name = "Pradeep";
((IDictionary
ExpandoObject class is inevitable in achieving the dynamic functionality offered in the new framework.
Hoever this falls shot when we need to have our custom dynamic objects.
DynamicObject class provide this functionality, which will be covered in the next article.
Hope this was Helpful.
Till Next time we connect......
Happy Coding
Comments
Post a Comment