mercredi 21 janvier 2015

How to create new data row field with unknown data type?

The question title is phrased poorly, so here's some background:


I'm using reflection to create a DataRow within a DataTable that has fields with the property name, value and type of another object, although I'm at a loss of how to set the type correctly, the following hasn't pleased intellisense:



Application app = Get(id, context); // Method args provide these

DataTable dt = new DataTable();
DataRow dr = dt.NewRow();

foreach (PropertyInfo p in app.GetType().GetProperties())
{
dr.SetField<p.PropertyType>(p.Name, p.GetValue(p, null)); // attempt 1
dr.SetField<typeof(p.GetType())>(p.Name, p.GetValue(p, null)); // attempt 2
dr.SetField<p.GetType()>(p.Name, p.GetValue(p, null)); // attempt 3
}

return dt;





Aucun commentaire:

Enregistrer un commentaire