Creating Dynamic Objects

Posted by Ramesh Durai on Stack Overflow See other posts from Stack Overflow or by Ramesh Durai
Published on 2012-09-14T15:11:36Z Indexed on 2012/09/14 15:38 UTC
Read the original article Hit count: 673

Filed under:
|
|
|
|

How to dynamically create objects?

string[] columnNames = { "EmpName", "EmpID", "PhoneNo" };
List<string[]> columnValues = new List<string[]>();
for (int i = 0; i < 10; i++)
{
    columnValues.Add(new[] { "Ramesh", "12345", "12345" });
}

List<Dictionary<string, object>> testData = new List<Dictionary<string, object>>();

foreach (string[] columnValue in columnValues)
{
    Dictionary<string, object> data = new Dictionary<string, object>();
    for (int j = 0; j < columnNames.Count(); j++)
    {
        data.Add(columnNames[j], columnValues[j]);
    }
    testData.Add(data);
}

Imaginary Class(Class is not available in code):

class Employee
{
    string EmpName { get;set; }
    string EmpID { get;set; }
    string PhoneNo { get;set; }
}

Note: Property/column names are dynamic.

Now I want to convert the List<Dictionary<string, object>> to a class of type List<object> (i.e) List<Employee>.

Is it Possible? Suggestions please.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET