How to build C# objects dynamically ?

In this blog post , lets have a look at the usage of ExpandoObject in C# to create the C# objects dynamically.

ExpandoObject was initially introduced in C# 4.0 which was also incidentally part of Dynamic Language Runtime (DLR).

How to build C# objects dynamically ?

In this blog post, let’s look at the usage of ExpandoObject in C# to create the C# objects dynamically.

ExpandoObject was introduced in C# 4.0, which was also incidentally part of Dynamic Language Runtime (DLR).

dynamic expandoObj = new ExpandoObject();
expandoObj.MovieName = "Bigil";

You might have a scenario where you want to create the object and the propertyname has to be dynamic.

For example , imagine that you want the property name MovieName1 , MovieName2 etc .

You can try the below

var expandoObj = new ExpandoObject() as IDictionary<string, Object>;
expandoObj.Add("MovieName1", "bigil");

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...