using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Common { public static class DataReaderToEntity { /// <summary> /// DataReader转泛型 /// </summary> /// <typeparam name="T">传入的实体类</typeparam> /// <param name="objReader">DataReader对象</param> /// <returns></returns> public static IList<T> ReaderToList<T>(IDataReader objReader) { using (objReader) { List<T> list = new List<T>(); //获取传入的数据类型 Type modelType = typeof(T); //遍历DataReader对象 while (objReader.Read()) { //使用与指定参数匹配最高的构造函数,来创建指定类型的实例 T model = Activator.CreateInstance<T>(); for (int i = 0; i < objReader.FieldCount; i++) { //判断字段值是否为空或不存在的值 if (!IsNullOrDBNull(objReader[i])) { //匹配字段名 PropertyInfo pi = modelType.GetProperty(objReader.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if (pi != null) { //绑定实体对象中同名的字段 pi.SetValue(model, CheckType(objReader[i], pi.PropertyType), null); } } } list.Add(model); } return list; } } /// <summary> /// 对可空类型进行判断转换(*要不然会报错) /// </summary> /// <param name="value">DataReader字段的值</param> /// <param name="conversionType">该字段的类型</param> /// <returns></returns> private static object CheckType(object value, Type conversionType) { if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition() == typeof(Nullable<>)) { if (value == null) return null; var nullableConverter = new System.ComponentModel.NullableConverter(conversionType); conversionType = nullableConverter.UnderlyingType; } return Convert.ChangeType(value, conversionType); } /// <summary> /// 判断指定对象是否是有效值 /// </summary> /// <param name="obj"></param> /// <returns></returns> private static bool IsNullOrDBNull(object obj) { return (obj == null || (obj is DBNull)) ? true : false; } /// <summary> /// DataReader转模型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="objReader"></param> /// <returns></returns> public static T ReaderToModel<T>(IDataReader objReader) { using (objReader) { if (objReader.Read()) { var modelType = typeof(T); var model = Activator.CreateInstance<T>(); for (var i = 0; i < objReader.FieldCount; i++) { if (!IsNullOrDBNull(objReader[i])) { var pi = modelType.GetProperty(objReader.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if (pi != null) { pi.SetValue(model, CheckType(objReader[i], pi.PropertyType), null); } } } return model; } } return default(T); } } }
时间: 2024-09-29 05:29:12