DataRow-to-generics, using reflection to turn query data directly into entities

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.