Build C# objects dynamically

Build C# objects dynamically

Through this blog post, we are going to learn on how to Build C# Objects dynamically and use them on the fly at runtime.

ExpandoObject has been part of C# from C# 4.0, which is a part of DLR (dynamic language runtime). ExpandoObject instances can add and remove members at runtime.

Use ExpandoObject to create an object that you can add properties, methods, and events to and be able to data bind to in a user interface.

We can use ExpandoObject to create an initial object to hold the Name and of Country a person.

dynamic expando = new ExpandoObject();

expando.Name = “Ravi”;

expando.Country = “USA”;

Once we have added properties directly, we can also add properties to our object in a more dynamic fashion using the method AddProperty. Let’s say we want to add email to this object

//Adding property dynamically to expando object

AddProperty(expando, “Email”, “naik899@gmail.com”);

If you look at the implementation of AddProperty, it takes advantage of the support that ExpandoObject has for IDictionary<string, object> and allows us to add the values at runtime. Here is the snippet for the same:

public static void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)

{

// ExpandoObject supports IDictionary so we can extend it like this

var expandoDict = expando as IDictionary<string, object>;

if (expandoDict.ContainsKey(propertyName)){

expandoDict[propertyName] = propertyValue;

} else{

expandoDict.Add(propertyName, propertyValue);

}

}

We can also add methods to the ExpandoObject by using the Func<> generic type which represents a method call. we can also define and add events to the ExpandoObject using the Action<> generic type. Even the AddEvent method takes the advantage of ExpandoObject‘s support of IDictionary<string, object>. Here is the snippet of the same

public static void AddEvent(ExpandoObject expando, string eventName, Action<object, EventArgs> handler)

{

var expandoDict = expando as IDictionary<string, object>;

if (expandoDict.ContainsKey(eventName)) {

expandoDict[eventName] = handler;

}

else{

expandoDict.Add(eventName, handler);

}

}

Finally, ExpandoObject supports INotifyPropertyChanged which is the foundation of data binding to properties in .NET.

ExpandoObject allows you to write code that is more readable than typical reflection code with GetProperty(“Field”) syntax. It can be useful when dealing with XML or JSON for quickly setting up a type to program against instead of always having to create data transfer objects. The ability for ExpandoObject to support data binding through INotifyProperyChanged is a huge win for anyone using WPF, MVC, or any other binding framework in .NET as it allows you to use these “objects on the fly” as well as other statically typed classes.

Here is how you can take use of ExpandoObject to not just create C# objects dynamically but to add lambda methods like Func<> and add Events and enable data binding to properties of the object.

Thanks for stopping by !!! Feel free to comment to this post or you can drop me an email at naik899@gmail.com

Leave a Reply