In foreword, this method uses reflection to turn the DataRow into a solid, because the reflection SetValue is said to be not good, everyone just look at it.
usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Reflection;usingSystem.Text;namespacewangsql.dbutility{ Public classDatamaphelper {Private enumModeltype {Value, String, Object, Reference}Private Staticmodeltype Getmodeltype (Type modeltype) {if(Modeltype.isvaluetype)//value type { returnModeltype.value; } Else if(Modeltype = =typeof(string))//reference type special type handling { returnmodeltype.string; } Else if(Modeltype = =typeof(Object))//reference type special type handling { returnModeltype.object; } Else//Reference type { returnmodeltype.reference; } } Public StaticList<t> datatabletolist<t>(DataTable table) {List<T> list =NewList<t>(); foreach(DataRow Iteminchtable. Rows) {list. ADD (Datarowtomodel<T>(item)); } returnlist; } Public StaticT datarowtomodel<t>(DataRow row) {T model; Type type=typeof(T); Modeltype Modeltype=Getmodeltype (type); Switch(modeltype) { CaseModeltype.value://value type{model=default(T); if(row[0] !=NULL) Model= (T) row[0]; } Break; CaseModeltype.string://reference Type C # also treat string as value type{model=default(T); if(row[0] !=NULL) Model= (T) row[0]; } Break; CaseModeltype.object://the reference type directly returns the value of the first column in the first row{model=default(T); if(row[0] !=NULL) Model= (T) row[0]; } Break; CaseModeltype.reference://Reference type{model= System.activator.createinstance<t> ();//A reference type must be instantiated on a generic type #regionMyregion//get the properties in modelpropertyinfo[] Modelpropertyinfos =type. GetProperties (); //iterate through each property of the model and assign a column corresponding to the DataRow foreach(PropertyInfo PiinchModelpropertyinfos) { //Get Property nameString name =Pi. Name; if(row. Table.Columns.Contains (name)) {//Non-generic if(!Pi. Propertytype.isgenerictype) {if(pi.) Propertytype.isenum) {pi. SetValue (model, Row[name],NULL); } Else{pi. SetValue (model,string. IsNullOrEmpty (Row[name]. ToString ())?NULL: Convert.changetype (Row[name], pi. PropertyType),NULL); } } //Generic nullable<> Else{Type generictypedefinition=Pi. Propertytype.getgenerictypedefinition (); //The model property is nullable and is assigned a null value if(Generictypedefinition = =typeof(nullable<>)) { //returns the underlying type parameter of the specified nullable typePi. SetValue (model,string. IsNullOrEmpty (Row[name]. ToString ())?NULL: Convert.changetype (Row[name], Nullable.getunderlyingtype (pi. PropertyType)),NULL); } } } } #endregion } Break; default: Model=default(T); Break; } returnmodel; } }}
Something
1. The performance can be improved by caching.
After each typeof (T), store its object-related information (generic properties, and so on) for the next read from the cache.
2. Improvements to SetValue.
You can use generic delegates to assign values to them.
3. Use emit
DataRow-to-generics, using reflection to turn query data directly into entities