Need to introduce namespace when using PropertyInfo: System.Reflection
// Create a table
DataTable dtResult = new DataTable();
// Create a collection of objects
List<ReturnData> retList = new ReturnData();
// Get the object of public property
PropertyInfo[] propertys = typeof(ReturnData).GetProperties();
// traverse the object of all the properties and create a column name
the foreach (PI in the propertys PropertyInfo)
{
// determines whether the attribute has Getter
IF (pi.CanRead!) Continue;
dtResult.Columns.Add (pi.Name, pi.PropertyType);
}
// Loop through the object class table, the data lines to a DataTable
the foreach (Item in retList var)
{
the propertys = item.GetType () GetProperties ();.
The DataRow the newRow dtResult.NewRow = ();
the foreach (PI in the propertys PropertyInfo)
{
IF (! pi.CanRead) Continue;
the newRow [pi.Name] = pi.GetValue (Item, null);
}
dtResult.Rows.Add (the newRow);
}
Note: according https://blog.51cto.com/ 13492397/2083194 rewrite, if infringement, please contact.
-------------------------------------------------------------------------------------------------------------------------------------
A rookie, this is just easy to learn and use personal future, the paper if wrong, pointing to welcome you big brother pointing!