标签:
实体类的字段类型要和数据库一致,不然可能会出现错误。
/// <summary> /// DataReader转泛型 /// </summary> /// <typeparam name="T">传入的实体类</typeparam> /// <param name="objReader">DataReader对象</param> /// <returns></returns> private static IList<T> ReaderToList<T>(this 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().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); } /// <summary> /// 判断指定对象是否是有效值 /// </summary> /// <param name="obj"></param> /// <returns></returns> private static bool IsNullOrDBNull(object obj) { return (obj == null || (obj is DBNull)) ? true : false; }
标签:
原文地址:http://www.cnblogs.com/liuruitao/p/4212619.html