DataSet is swapped for generic collection 222

Source: Internet
Author: User

        #regionDataSet is swapped for generic collection/// <summary>          ///take advantage of reflection and generics/// </summary>          /// <param name= "DT" ></param>          /// <returns></returns>           Public StaticList<t> converttolist<t>(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= (T) activator.createinstance (typeof(T)); //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, value, NULL);Pi. SetValue (t, Dynamicconvert (value, pi).                    PropertyType)); }                }                //object is added to the generic collectionts.            ADD (t); }            returnts; }        /// <summary>        ///Universal Conversion method to convert values to the target type whenever possible/// </summary>         Public Static ObjectDynamicconvert (Objectvalue, type type) {            if(Value = =NULL&& type. Isgenerictype)returnactivator.createinstance (type); if(Value = =NULL)return NULL; if(Type = = value.) GetType ())returnvalue; if(type. Isenum) {if(Value is string)                    returnEnum.parse (Type, value as string); Else                    returnenum.toobject (type, value); }            if(!type. Isinterface &&type. Isgenerictype) {if(value.) GetType () = =typeof(string) && convert.tostring (value) = ="")                {                    return NULL; }                //DBNull returns null to go back                if(Convert.isdbnull (value)) {return NULL; } Type Innertype= Type. GetGenericArguments () [0]; ObjectInnervalue =Dynamicconvert (value, Innertype); returnType. InvokeMember (NULL, Bindingflags.createinstance,NULL,NULL,New Object[] {innervalue}); }            //if it is a value type, and the current value is an empty string, or DBNull, the default value is returned            if(Type. Isvaluetype && (value. GetType () = =typeof(string) && convert.tostring (value) = ="") ||Convert.isdbnull (value)) {                returnType. InvokeMember (NULL, Bindingflags.createinstance,NULL,NULL,New Object[] { }); }            //if (value is string && type = = typeof (Guid)) return new Guid (value as String); //if (value is string && type = = typeof (Version)) return new Version (value as String);            if(! (Value isIConvertible))returnvalue; returnConvert.changetype (value, type); }        /// <summary>        ///Universal Conversion method to convert values to the target type whenever possible/// </summary>         Public StaticT dynamicconvert<t> (Objectvalue) {T result= (T) Dynamicconvert (value,typeof(T)); returnresult; }       #endregion

DataSet is swapped for generic collection 222

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.