Thursday, October 4, 2012

Expando Object

The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members.

Creating a dynamic instance:
       dynamic Student = new ExpandoObject();
       Student.ID = 1;
       Student.Name = "John";
Student.Grade = 10;


Creating a nested dynamic instance:
       Student.Addess = new ExpandoObject();
       Student.Addess.City = "London";
       Student.Addess.Country = "UK";
       Student.Addess.StreetNo = 123456;
Dynamically binding an Event
Student.Click += new EventHandler(SampleHandler);
 
private void SampleHandler(object sender, EventArgs e)
{
//throw new NotImplementedException();
}
Pass parameter as dynamic.
public void Write(dynamic Student)
{
//do stuff
}

No comments: