Creating Dynamic Objects
- by Ramesh Durai
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.