Returns the able set reflection list. <m>
/// <Summary> /// obtain the list based on the reflection of the able set <m> /// </Summary> /// <typeparam name = "M"> generic Entity </typeparam> // <Param name = "DT"> datatable </param> // <returns> object set </returns> Private Static list <m> setvaluerow <m> (datatable DT) where M: New () {list <m> List = new list <m> (); Type type; propertyinfo P; m; foreach (datarow row in DT. rows) {M = new M (); type = m. getType (); foreach (datacolumn Col in DT. colu MNS) {// obtain the attribute P = type of a field. getproperty (Col. columnname); // no corresponding attribute in the object if (P = NULL) continue; string coldbtype = row [col. columnname]. getType (). fullname; // If (coldbtype! = "System. dbnull ") {Switch (P. propertytype. fullname) {Case "system. int64 ": // converts data types of different databases. For example, the number (2) of Oracle should be converted to int32 instead of the default decemal p. setvalue (M, convert. toint64 (row [col. columnname]), null); break; Case "system. int32 ": P. setvalue (M, convert. toint32 (row [col. columnname]), null); break; Case "system. int16 ": P. setvalue (M, convert. toint16 (row [col. columnname]), null); break; Case "system. string ": P. setvalue (M, convert. tostring (row [col. columnname]), null); break; Case "system. decimal ": P. setvalue (M, convert. todecimal (row [col. columnname]), null); break; Case "system. datetime ": P. setvalue (M, row [col. columnname], null); break; Case "system. double ": P. setvalue (M, convert. todouble (row [col. columnname]), null); break; Case "system. boolean ": P. setvalue (M, convert. toboolean (row [col. columnname]), null); break; Case "system. byte ": P. setvalue (M, convert. tobyte (row [col. columnname]), null); break; default: P. setvalue (M, row [col. columnname], null); break ;}} list. add (m);} return list ;}
View code
Converts an ilist collection class to a datatable
/// <Summary> /// convert the ilist collection class to a datatable // </Summary> /// <Param name = "list"> set </param> // /<returns> </returns> Public static datatable ilisttodatatable (ilist List) {datatable result = new datatable (); If (list. count> 0) {propertyinfo [] propertys = list [0]. getType (). getproperties (); foreach (propertyinfo PI in propertys) {result. columns. add (Pi. name, Pi. propertytype);} For (INT I = 0; I <list. count; I ++) {arraylist templist = new arraylist (); foreach (propertyinfo PI in propertys) {object OBJ = pi. getvalue (list [I], null); templist. add (OBJ);} object [] array = templist. toarray (); result. loaddatarow (array, true) ;}} return result ;}
View code
Convert the list <m> collection class to a able
/// <Summary> /// convert the list <m> collection class to a datatable // </Summary> /// <Param name = "list"> set </Param >/// <returns> </returns> Public static datatable ilisttodatatable <m> (list <m> List) {datatable result = new datatable (); If (list. count> 0) {propertyinfo [] propertys = list [0]. getType (). getproperties (); foreach (propertyinfo PI in propertys) {result. columns. add (Pi. name, Pi. propertytype);} For (INT I = 0; I <list. count; I ++) {arraylist templist = new arraylist (); foreach (propertyinfo PI in propertys) {object OBJ = pi. getvalue (list [I], null); templist. add (OBJ);} object [] array = templist. toarray (); result. loaddatarow (array, true) ;}} return result ;}
View code
Reflection list <m> to datatable | reflection ilist to datatable | reflection datatable to list <m>