/// Convert a collection class to a able
/// </Summary>
/// <Param name = "list"> set </param>
/// <Returns> </returns>
Public static datatable todatatable (ilist List)
{
Datatable result = new datatable ();
If (list. Count> 0)
{
Propertyinfo [] propertys = list [0]. GetType (). getproperties ();
Foreach (propertyinfo PI in propertys)
{
Type coltype = pi. propertytype; If (coltype. isgenerictype) & (coltype. getgenerictypedefinition () = typeof (nullable <>) // avoid null errors
{
Coltype = coltype. getgenericarguments () [0];
}
If (PI. Name! = "Datatable_action _") // avoid a datatype error that requires a valid column, mainly for custom data types.
{
Result. Columns. Add (New datacolumn (PI. Name, coltype ));
}
}
For (INT I = 0; I <list. Count; I ++)
{
Arraylist templist = new arraylist ();
Foreach (propertyinfo PI in propertys)
{
If (PI. Name! = "Datatable_action _") // avoid a datatype error that requires a valid column, mainly for custom data types.
{
Object OBJ = pi. getvalue (list [I], null) = NULL? Dbnull. Value: PI. getvalue (list [I], null); // avoid null errors
// Object OBJ = pi. getvalue (list [I], null );
Templist. Add (OBJ );
}
}
Object [] array = templist. toarray ();
Result. loaddatarow (array, true );
}
}
Return result;
}