DataReader-turn generic

Source: Internet
Author: User

The entity class must have a field type that is consistent with the database, or an error may occur.

  /// <summary>        ///DataReader-turn generic/// </summary>        /// <typeparam name= "T" >the entity class passed in</typeparam>        /// <param name= "objreader" >DataReader Object</param>        /// <returns></returns>        Private StaticIlist<t> readertolist<t> ( ThisIDataReader objreader) {            using(objreader) {List<T> list =NewList<t>(); //get the data type passed inType Modeltype =typeof(T); //traversing DataReader Objects                 while( ()) {//creates an instance of the specified type by using the constructor that matches the specified argument to the highestT model = activator.createinstance<t>();  for(inti =0; i < Objreader.fieldcount; i++)                    {                        //A value that determines whether the field value is empty or does not exist                        if(!Isnullordbnull (Objreader[i])) {                            //Match Field namePropertyInfo pi = modeltype.getproperty (Objreader.getname (i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance |bindingflags.ignorecase); if(Pi! =NULL)                            {                                //a field with the same name in the bound entity objectPi. SetValue (model, Checktype (objreader[i), Pi. PropertyType),NULL); }}} list.                ADD (model); }                returnlist; }        }        /// <summary>        ///Convert a nullable type (* otherwise an error)/// </summary>        /// <param name= "value" >the value of the DataReader field</param>        /// <param name= "Conversiontype" >the type of the field</param>        /// <returns></returns>        Private Static ObjectChecktype (Objectvalue, Type conversiontype) {            if(Conversiontype.isgenerictype && conversiontype.getgenerictypedefinition (). Equals (typeof(nullable<>)))            {                if(Value = =NULL)                    return NULL; System.ComponentModel.NullableConverter Nullableconverter=NewSystem.ComponentModel.NullableConverter (Conversiontype); Conversiontype=Nullableconverter.underlyingtype; }            returnConvert.changetype (value, conversiontype); }        /// <summary>        ///determines whether the specified object is a valid value/// </summary>        /// <param name= "obj" ></param>        /// <returns></returns>        Private Static BOOLIsnullordbnull (Objectobj) {            return(obj = =NULL|| (obj isDBNull))?true:false; }

DataReader-turn generic

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: 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.