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.
In C++, the objects can be created at run-time. C++ supports two operators new and delete to perform memory allocation and de-allocation. These types of objects are called dynamic objects. The new operator is used to create objects dynamically and the delete operator is used to delete objects dynamically.
A dynamic object is created using a "new" operator that returns a pointer to the newly constructed object and is destructed by a "delete" operator. A pointer variable is used to hold the pointer to the object that is returned by the "new" operator.
Dynamic objects provide another way, other than the Object type, to late bind to an object at run time. A dynamic object exposes members such as properties and methods at run time by using dynamic interfaces that are defined in the System. Dynamic namespace. You can use the classes in the System.
In Java objects are created dynamically i.e. at runtime after successful compilation of the program during execution. new Object(); refers to the call to default constructor of the class Object.
Using an anonymous object (if you know the properties you want to project):
var employees =
(from dict in testData
select new
{
EmpName = dict["EmpName"] as string,
EmpID= dict["EmpID"] as string,
PhoneNo=dict["PhoneNo"] as string
}).ToList();
Or, using System.Dynamic.Expando (if you need to dynamically project unknown column names):
string[] columnNames = { "EmpName", "EmpID", "PhoneNo" };
List<string[]> columnValues = new List<string[]>();
for (int i = 0; i < 10; i++)
{
columnValues.Add(new[] { "Ramesh", "12345", "12345" });
}
var testData = new List<ExpandoObject>();
foreach (string[] columnValue in columnValues)
{
dynamic data = new ExpandoObject();
for (int j = 0; j < columnNames.Count(); j++)
{
((IDictionary<String,Object>)data).Add(columnNames[j], columnValue[j]);
}
testData.Add(data);
}
Edited to suggest "Emit".
In light of the fact that you don't even know the column names, I would use Reflection.Emit to first create the Employee class on the fly. See http://msdn.microsoft.com/en-us/library/3y322t50(v=vs.100).aspx for information on how to use Emit. The pseudo code will then be:
ReflectionEmit("Employee", columns);
List<object> newList = testData.Select<object>(p => {
var employee = ReflectionInstantiate("Employee");
foreach column in columns
Reflection.SetProperty(employee, column, p[column]);
});
The real code will be a little more complicated as Emit is not straightforward. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With