Let’s say list of member names stored in an array like below,
string[] myMembers = { "ChapterName", "Medium", "LastName", "OrderID" };
I wrote the following code to generate dynamic class and members along with random values.
var myDynamicClassList = new List<ExpandoObject>();
foreach (var MemberName in myMembers)
{
dynamic dynamicClass = new ExpandoObject();
dynamicClass.MemberName = new Random().Next();
myDynamicClassList.Add(dynamicClass);
}
In order to display output of the above myDynamicClassList
I wrote the following code.
foreach (var x in myDynamicClassList)
{
foreach (var property in (IDictionary<String, Object>)x)
{
Console.WriteLine(property.Key + ": " + property.Value);
}
}
Showing output like this
MemberName : 123465461
MemberName : 564613611
MemberName : 134654321
MemberName : 786451214
But Instead of above output I am expecting the output like below
ChapterName : 123465461
Medium : 564613611
LastName : 134654321
OrderID : 786451214
Here is my question, is it possible to add dynamic member name to a dynamic class in c#. If it is possible please let me know, if not please guide me to complete this job.
I really appreciate your help in advanced.