Table-to-list generic array

Source: Internet
Author: User

Convert that piece.

usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Reflection;usingSystem.Text;namespaceapp.util{ Public classDtconvertlisthelper<t>whereT:New()    {        /// <summary>          ///take advantage of reflection and generics/// </summary>          /// <param name= "DT" ></param>          /// <returns></returns>           Public StaticList<t>converttolist (DataTable dt) {//Defining Collectionslist<t> ts =NewList<t>(); //get the type of this modelType type =typeof(T); //define a temporary variable            stringTempname =string.            Empty; //traverse all data rows in a DataTable            foreach(DataRow Drinchdt. Rows) {T T=NewT (); //get the public properties of this modelpropertyinfo[] Propertys =T.gettype ().                GetProperties (); //Traverse All properties of this object                foreach(PropertyInfo Piinchpropertys) {Tempname= Pi. Name;//Assigning a property name to a temporary variable//Check if DataTable contains this column (column name = = Property Name of object)                    if(dt. Columns.contains (Tempname)) {//determine if this property has a setter                        if(!pi. CanWrite)Continue;//This property is not writable and jumps directly out of//Take value                        ObjectValue =Dr[tempname]; //if not NULL, the property assigned to the object                        if(Value! =dbnull.value) {//Pi. SetValue (T,convert.changetype (value,pi. PropertyType), null);                            if(!Pi. Propertytype.isgenerictype) {//Non-genericPi. SetValue (T,string. IsNullOrEmpty (value. ToString ())?NULL: Convert.changetype (value, pi. PropertyType),NULL); }                            Else                            {                                //Generic nullable<>Type generictypedefinition =Pi.                                Propertytype.getgenerictypedefinition (); if(Generictypedefinition = =typeof(nullable<>) ) {pi. SetValue (T,string. IsNullOrEmpty (value. ToString ())?NULL: Convert.changetype (value, Nullable.getunderlyingtype (PI). PropertyType)),NULL); }                            }                        }                    }                }                //object is added to the generic collectionts.            ADD (t); }            returnts; }    }}

Table-to-list generic array

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.