使用泛型和反射实现IDataReader转实体

    #region 使用反射和泛型实现IDataReader向实体的转化(扩展方法)
    /// <summary>
    /// 使用反射和泛型实现IDataReader向实体的转化(扩展方法)
    /// </summary>
    public static class ReaderHelper
    {
        #region IDataReader 转实体
        /// <summary>
        /// IDataReader 转实体
        /// </summary>
        /// <typeparam name="T">传入的实体类</typeparam>
        /// <param name="row">数据库返回IDataReader</param>
        /// <returns></returns>
        public static T ReaderToModel<T>(this IDataReader row)
        {
            try
            {
                // 1、使用与指定参数匹配最高的构造函数,来创建指定类型的实例
                Type modelType = typeof(T);
                T model = Activator.CreateInstance<T>();
                for (int i = 0; i < row.FieldCount; i++)
                {
                    // 2、判断字段值是否为空或不存在的值
                    if (!(row[i] == null || row[i] is DBNull))
                    {
                        // 3、匹配字段名
                        PropertyInfo pi = modelType.GetProperty(row.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                        if (pi != null)
                        {
                            // 4、绑定实体对象中同名的字段 
                            pi.SetValue(model, CheckType(row[i], pi.PropertyType), null);
                        }
                    }
                }

                return model;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region IDataReader 转实体列表
        /// <summary>
        /// DataReader转泛型
        /// </summary>
        /// <typeparam name="T">传入的实体类</typeparam>
        /// <param name="objReader">DataReader对象</param>
        /// <returns></returns>
        public static List<T> ReaderToList<T>(this IDataReader objReader)
        {
            try
            {
                using (objReader)
                {
                    List<T> list = new List<T>();

                    //1、获取传入的数据类型
                    Type modelType = typeof(T);

                    //2、遍历DataReader对象
                    while (objReader.Read())
                    {
                        // 3、使用与指定参数匹配最高的构造函数,来创建指定类型的实例
                        T model = Activator.CreateInstance<T>();
                        for (int i = 0; i < objReader.FieldCount; i++)
                        {
                            // 4、判断字段值是否为空或不存在的值
                            if (!(objReader[i] == null || objReader[i] is DBNull))
                            {
                                // 5、匹配字段名
                                PropertyInfo pi = modelType.GetProperty(objReader.GetName(i), BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                                if (pi != null)
                                {
                                    // 6、绑定实体对象中同名的字段  
                                    pi.SetValue(model, CheckType(objReader[i], pi.PropertyType), null);
                                }
                            }
                        }
                        list.Add(model);
                    }
                    return list;
                }
            }
            catch
            {
                throw;
            }
        }
        #endregion

        #region 对可空类型进行判断
        /// <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().Equals(typeof(Nullable<>)))
            {
                if (value == null)
                    return null;
                System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
                conversionType = nullableConverter.UnderlyingType;
            }
            return Convert.ChangeType(value, conversionType);
        }
        #endregion

    }
    #endregion

借鉴自:https://www.cnblogs.com/zhuiyi/archive/2012/09/19/2694438.html

原文地址:https://www.cnblogs.com/highest/p/8461175.html