Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. data; <br/> using system. reflection; <br/> using system. collections; </P> <p> namespace certificatereport. utility <br/> {<br/> /// <summary> <br/> // represents some utility functions for Object-datatable manipulation <br/> // </Summary> <br/> Public sealed class otconverter <br/> {</P> <p> // <summary> <br/> /// Convert an datarow to an object <br/> /// </Summary> <br/> /// <typeparam name = "T"> </typeparam> <br/> /// <Param name = "row"> </param> <br/> /// <returns> </returns> <br/> Public static t converttoobject <t> (datarow row) where T: New () <br/>{< br/> Object OBJ = new T (); <br/> If (row! = NULL) <br/>{< br/> datatable T = row. table; <br/> GetObject (T. columns, row, OBJ); <br/>}< br/> If (OBJ! = NULL & obj is t) <br/> return (t) OBJ; <br/> else <br/> return default (t ); </P> <p >}</P> <p> // <summary> <br/> // convert a data table to an objct list <br/> /// </Summary> <br/> // <typeparam name = "T"> </typeparam> <br/> // <Param name = "T"> </param> <br/> // <returns> </returns> <br/> Public static list <t> converttabletoobject <t> (datatable T) where T: New () <br/>{< br/> List <t> List = new list <T> (); <br/> foreach (datarow row in T. rows) <br/>{< br/> t obj = converttoobject <t> (ROW); <br/> list. add (OBJ); <br/>}</P> <p> return list; </P> <p >}</P> <p> // <summary> <br/> // convert object collection to an data table <br/>/ /// </Summary> <br/> /// <Param name = "list"> </param> <br/> /// <returns> </returns> <br/> Public static datatable genericlisttodatatable (Object List) <br/> {<br/> datatabl E dt = NULL; <br/> type listtype = List. getType (); <br/> If (listtype. isgenerictype) <br/>{< br/> // determine the underlying type the list <> contains <br/> type elementtype = listtype. getgenericarguments () [0]; </P> <p> // create empty table -- give it a name in case <br/> // it needs to be serialized <br/> dt = new datatable (elementtype. name + "list"); </P> <p> // define the table -- add a column for EA Ch Public <br/> // property or field <br/> memberinfo [] miarray = elementtype. getmembers (<br/> bindingflags. public | bindingflags. instance); <br/> foreach (memberinfo MI in miarray) <br/>{< br/> If (MI. membertype = membertypes. property) <br/>{< br/> propertyinfo Pi = mi as propertyinfo; <br/> DT. columns. add (Pi. name, Pi. propertytype); <br/>}< br/> else if (MI. membertype = membertypes. field) <br />{< Br/> fieldinfo Fi = mi as fieldinfo; <br/> DT. columns. add (Fi. name, Fi. fieldtype); <br/>}</P> <p> // populate the table <br/> ilist IL = List as ilist; <br/> foreach (Object Record in IL) <br/>{< br/> int I = 0; <br/> object [] fieldvalues = new object [DT. columns. count]; <br/> foreach (datacolumn C in DT. columns) <br/>{< br/> memberinfo MI = elementtype. getmember (C. columnname) [0]; <br /> If (MI. membertype = membertypes. property) <br/>{< br/> propertyinfo Pi = mi as propertyinfo; <br/> fieldvalues [I] = pi. getvalue (record, null); <br/>}< br/> else if (MI. membertype = membertypes. field) <br/>{< br/> fieldinfo Fi = mi as fieldinfo; <br/> fieldvalues [I] = Fi. getvalue (record); <br/>}< br/> I ++; <br/>}< br/> DT. rows. add (fieldvalues); <br/>}< br/> return DT; <br/>}</P> <P> # region "internal methods" <br/> Private Static void GetObject (datacolumncollection cols, datarow DR, object OBJ) <br/>{< br/> type T = obj. getType (); // This is used to do the reflection </P> <p> propertyinfo [] props = T. getproperties (); <br/> foreach (propertyinfo pro in props) <br/>{< br/> If (cols. contains (Pro. name) <br/>{< br/> pro. setvalue (OBJ, <br/> Dr [pro. name] = dbnull. value? Null: Dr [pro. name], <br/> null); <br/>}</P> <p> // For (int32 I = 0; I <= cols. count-1; I ++) <br/> // {<br/> // try <br/> // {// note the datarow column names must match exactly (including case) to the object property names <br/> // T. invokemember (Cols [I]. columnname <br/> //, bindingflags. setproperty <br/> //, null <br/> //, OBJ <br/> //, new object [] {Dr [Cols [I]. columnname]}); <br />///} <Br/> // catch (exception ex) <br/> // {// usually you are getting here because a column doesn't exist or it is null <br/> // If (ex. tostring ()! = NULL) <br/> // {<br/> //} <br/>//} <br/> //}; <br/>}</P> <p> # endregion </P> <p >}< br/>