Using system;
Using system. Collections. Generic;
Using system. Data;
Using system. reflection;
Namespace aqioo. modules. Consult. Extensions
{
/// <Summary>
/// Summary description for convertentity1
/// </Summary>
Public static class idatareaderext
{
Public static t readertomodel <t> (this idatareader Dr)
{
// Try
//{
Using (DR)
{
If (dr. Read ())
{
Type modeltype = typeof (t );
Int COUNT = dr. fieldcount;
T model = activator. createinstance <t> ();
For (INT I = 0; I <count; I ++)
{
If (! Isnullordbnull (Dr [I])
{
Propertyinfo Pi = modeltype. getproperty (getpropertyname (dr. getname (I), bindingflags. getproperty | bindingflags. Public | bindingflags. instance | bindingflags. ignorecase );
If (Pi! = NULL)
{
Pi. setvalue (model, hacktype (Dr [I], Pi. propertytype), null );
}
}
}
Return Model;
}
}
Return default (t );
//}
// Catch (exception ex)
//{
// Return default (t );
//}
}
Public static ilist <t> readertolist <t> (this idatareader Dr)
{
Using (DR)
{
List <t> List = new list <t> ();
Type modeltype = typeof (t );
Int COUNT = dr. fieldcount;
While (dr. Read ())
{
T model = activator. createinstance <t> ();
For (INT I = 0; I <count; I ++)
{
If (! Isnullordbnull (Dr [I])
{
Propertyinfo Pi = modeltype. getproperty (getpropertyname (dr. getname (I), bindingflags. getproperty | bindingflags. Public | bindingflags. instance | bindingflags. ignorecase );
If (Pi! = NULL)
{
Pi. setvalue (model, hacktype (Dr [I], Pi. propertytype), null );
}
}
}
List. Add (model );
}
Return list;
}
}
// This class determines whether to convert the null type. Otherwise, an error is returned.
Private Static object hacktype (object value, type conversiontype)
{
If (conversiontype. isgenerictype & conversiontype. getgenerictypedefinition (). Equals (typeof (nullable <> )))
{
If (value = NULL)
Return NULL;
System. componentmodel. nullableconverter = new system. componentmodel. nullableconverter (conversiontype );
Conversiontype = nullableconverter. underlyingtype;
}
Return convert. changetype (value, conversiontype );
}
Private Static bool isnullordbnull (Object OBJ)
{
Return (OBJ = NULL | (obj is dbnull ))? True: false;
}
// Obtain the bean attribute name corresponding to the DB column
Private Static string getpropertyname (string column)
{
Column = column. tolower ();
String [] narr = column. Split ('_');
Column = "";
For (INT I = 0; I <narr. length; I ++)
{
If (narr [I]. length> 1)
{
Column + = narr [I]. substring (0, 1). toupper () + narr [I]. substring (1 );
}
Else
{
Column + = narr [I]. substring (0, 1). toupper ();
}
}
Return column;
}
}
}