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(Objreader.read ()) {//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