C# DataReader反射获取实体类

在网上查了一下,发现有些人写的效率不高.自己修改了一下.
根据select出来的字段去反射.

        public static T ReaderToModel<T>(IDataReader dr)
        {
            try
            {
                using (dr)
                {
                    if (dr.Read())
                    {
                        Type modelType = typeof(T);
                        T model = Activator.CreateInstance<T>();
                        for (int i = 0; i < dr.FieldCount; i++)
                        {
                            PropertyInfo pi = modelType.GetProperty(GetPropertyName(dr.GetName(i)));
                            pi.SetValue(model, HackType(dr[i], pi.PropertyType), null);
                        }
                        return model;
                    }
                }
                return default(T);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<T> ReaderToList<T>(IDataReader dr)
        {
            using (dr)
            {               
                List<T> list = new List<T>();
                Type modelType = typeof(T);
                while (dr.Read())
                {
                    T model = Activator.CreateInstance<T>();
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        PropertyInfo pi = modelType.GetProperty(GetPropertyName(dr.GetName(i)));
                        pi.SetValue(model, HackType(dr[i], pi.PropertyType), null);
                    }
                    list.Add(model);
                }
                return list;
            }
        }
        //这个类对可空类型进行判断转换,要不然会报错
        private static object HackType(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);
        }

        private static bool IsNullOrDBNull(object obj)
        {
            return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString())) ? true : false;
        }

        //取得DB的列对应bean的属性名
        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;
        }

你可能感兴趣的:(C++,c,bean,C#)