usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.Common;usingSystem.Reflection;///summary///实体阅读器类,可以从DataTable中或者DbDataReader的实例中将数据转换成对应的示例///说明:(1)任何人都可以免费使用,请尽量保持此段说明。///(2)这个版本还不是最终版本,有任何意见或建议请到处留言。////summarypublicsealedclassEntityReader{privateconstBindingFlagsBindingFlag=BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance;//将类型与该类型所有的可写且未被忽略属性之间建立映射privatestaticDictionaryType,Dictionarystring,PropertyInfopropertyMappings=newDictionaryType,Dictionarystring,PropertyInfo();//存储NullableT与T的对应关系privatestaticDictionaryType,TypegenericTypeMappings=newDictionaryType,Type();staticEntityReader(){genericTypeMappings.Add(typeof(Byte?),typeof(Byte));genericTypeMappings.Add(typeof(SByte?),typeof(SByte));genericTypeMappings.Add(typeof(Char?),typeof(Char));genericTypeMappings.Add(typeof(Boolean?),typeof(Boolean));genericTypeMappings.Add(typeof(Guid?),typeof(Guid));genericTypeMappings.Add(typeof(Int16),typeof(Int16));genericTypeMappings.Add(typeof(UInt16),typeof(UInt16));genericTypeMappings.Add(typeof(Int32),typeof(Int32));genericTypeMappings.Add(typeof(UInt32),typeof(UInt32));genericTypeMappings.Add(typeof(Int64),typeof(Int64));genericTypeMappings.Add(typeof(UInt64),typeof(UInt64));genericTypeMappings.Add(typeof(Single),typeof(Single));genericTypeMappings.Add(typeof(Double),typeof(Double));genericTypeMappings.Add(typeof(Decimal),typeof(Decimal));genericTypeMappings.Add(typeof(DateTime),typeof(DateTime));genericTypeMappings.Add(typeof(TimeSpan),typeof(TimeSpan));genericTypeMappings.Add(typeof(Enum),typeof(Enum));}///summary///将DataTable中的所有数据转换成List>T<集合////summary///typeparamname=TDataTable中每条数据可以转换的数据类型/typeparam///paramname=dataTable包含有可以转换成数据类型T的数据集合/param///returns/returnspublicstaticListTGetEntitiesT(DataTabledataTable)whereT:new(){if(dataTable==null){thrownewArgumentNullException(dataTable);}//如果T的类型满足以下条件:字符串、ValueType或者是NullableValueTypeif(typeof(T)==typeof(string)||typeof(T).IsValueType){returnGetSimpleEntitiesT(dataTable);}else{returnGetComplexEntitiesT(dataTable);}}///summary///将DbDataReader中的所有数据转换成List>T<集合////summary///typeparamname=TDbDataReader中每条数据可以转换的数据类型/typeparam///paramname=dataTable包含有可以转换成数据类型T的DbDataReader实例/param///returns/returnspublicstaticListTGetEntitiesT(DbDataReaderreader)whereT:new(){ListTlist=newListT();if(reader==null){thrownewArgumentNullException(reader);}//如果T的类型满足以下条件:字符串、ValueType或者是NullableValueTypeif(typeof(T)==typeof(string)||typeof(T).IsValueType){returnGetSimpleEntitiesT(reader);}else{returnGetComplexEntitiesT(reader);}}///summary///从DataTable中将每一行的第一列转换成T类型的数据////summary///typeparamname=T要转换的目标数据类型/typeparam///paramname=dataTable包含有可以转换成数据类型T的数据集合/param///returns/returnsprivatestaticListTGetSimpleEntitiesT(DataTabledataTable)whereT:new(){ListTlist=newListT();foreach(DataRowrowindataTable.Rows){list.Add((T)GetValueFromObject(row[0],typeof(T)));}returnlist;}///summary///将指定的Object的值转换为指定类型的值。////summary///paramname=value实现IConvertible接口的Object,或者为null/param///paramname=targetType要转换的目标数据类型/param///returns/returnsprivatestaticobjectGetValueFromObject(objectvalue,TypetargetType){if(targetType==typeof(string))//如果要将value转换成string类型{returnGetString(value);}elseif(targetType.IsGenericType)//如果目标类型是泛型{returnGetGenericValueFromObject(value,targetType);}else//如果是基本数据类型(包括数值类型、枚举和Guid){returnGetNonGenericValueFromObject(value,targetType);}}///summary///从DataTable中读取复杂数据类型集合////summary///typeparamname=T要转换的目标数据类型/typeparam///paramname=dataTable包含有可以转换成数据类型T的数据集合/param///returns/returnsprivatestaticListTGetComplexEntitiesT(DataTabledataTable)whereT:new(){if(!propertyMappings.ContainsKey(typeof(T))){GenerateTypePropertyMapping(typeof(T));}ListTlist=newListT();Dictionarystring,PropertyInfoproperties=propertyMappings[typeof(T)];//Dictionarystring,intpropertyColumnOrdinalMapping=GetPropertyColumnIndexMapping(dataTable.Columns,properties);Tt;foreach(DataRowrowindataTable.Rows){t=newT();foreach(KeyValuePairstring,PropertyInfoiteminproperties){//intordinal=-1;//if(propertyColumnOrdinalMapping.TryGetValue(item.Key,outordinal))//{//item.Value.SetValue(t,GetValueFromObject(row[ordinal],item.Value.PropertyType),null);//}item.Value.SetValue(t,GetValueFromObject(row[item.Key],item.Value.PropertyType),null);}list.Add(t);}returnlist;}///summary///从DbDataReader的实例中读取复杂的数据类型////summary///typeparamname=T要转换的目标类/typeparam///paramname=readerDbDataReader的实例/param///returns/returnsprivatestaticListTGetComplexEntitiesT(DbDataReaderreader)whereT:new(){if(!propertyMappings.ContainsKey(typeof(T)))//检查当前是否已经有该类与类的可写属性之间的映射{GenerateTypePropertyMapping(typeof(T));}ListTlist=newListT();Dictionarystring,PropertyInfoproperties=propertyMappings[typeof(T)];//Dictionarystring,intpropertyColumnOrdinalMapping=GetPropertyColumnIndexMapping(reader,properties);Tt;while(reade