// Convert dataset to object class
Public static ilist <t> fillmodel (Dataset DS)
{
List <t> L = new list <t> ();
T model = default (t );
If (Ds. Tables [0]. Columns [0]. columnname = "rowid ")
{
DS. Tables [0]. Columns. Remove ("rowid ");
}
Foreach (datarow DR in DS. Tables [0]. Rows)
{
Model = activator. createinstance <t> ();
Foreach (datacolumn DC in dr. Table. columns)
{
Propertyinfo Pi = model. GetType (). getproperty (DC. columnname );
If (Dr [DC. columnname]! = Dbnull. value)
Pi. setvalue (model, Dr [DC. columnname], null );
Else
Pi. setvalue (model, null, null );
}
L. Add (model );
}
Return L;
}
/// <Summary>
/// Convert an object class to a able
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "I _objlist"> </param>
/// <Returns> </returns>
Public static datatable fill <t> (ilist <t> objlist)
{
If (objlist = NULL | objlist. Count <= 0)
{
Return NULL;
}
Datatable dt = new datatable (typeof (T). Name );
Datacolumn column;
Datarow row;
System. reflection. propertyinfo [] mypropertyinfo = typeof (T). getproperties (bindingflags. Public | bindingflags. instance );
Foreach (t in objlist)
{
If (t = NULL)
{
Continue;
}
Row = DT. newrow ();
For (INT I = 0, j = mypropertyinfo. length; I <j; I ++)
{
System. reflection. propertyinfo Pi = mypropertyinfo [I];
String name = pi. Name;
If (Dt. Columns [name] = NULL)
{
Column = new datacolumn (name, Pi. propertytype );
DT. Columns. Add (column );
}
Row [name] = pi. getvalue (T, null );
}
DT. Rows. Add (ROW );
}
Return DT;
}