热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

利用反射实现通用的DataReader转List、DataReader转实体类

发布时间:2010年03月04日点击数: 佚名

dataReader转list和转model有时候经常用,为了偷懒嘛,少写代码倒是不少,用缓存基本上也可以把性能补过来吧,反正我没有测试过,哪位同仁测试过,大家可以研究研究~~

  1. public static T ReaderToModel<T>(IDataReader dr) 
  2.     { 
  3.         try 
  4.         { 
  5.             using (dr) 
  6.             { 
  7.                 if (dr.Read()) 
  8.                 { 
  9.                     List<string> list = new List<string>(dr.FieldCount); 
  10.                     for (int i = 0; i < dr.FieldCount; i++) 
  11.                     { 
  12.                         list.Add(dr.GetName(i).ToLower()); 
  13.                     } 
  14.                     T model = Activator.CreateInstance<T>(); 
  15.                     foreach (PropertyInfo pi in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance)) 
  16.                     { 
  17.                         if (list.Contains(pi.Name.ToLower())) 
  18.                         { 
  19.                             if (!IsNullOrDBNull(dr[pi.Name])) 
  20.                             { 
  21.                                 pi.SetValue(model, HackType(dr[pi.Name], pi.PropertyType), null); 
  22.                             } 
  23.                         } 
  24.                     } 
  25.                     return model; 
  26.                 } 
  27.             } 
  28.             return default(T); 
  29.         } 
  30.         catch (Exception ex) 
  31.         { 
  32.             throw ex; 
  33.         } 
  34.     } 
  35.  
  36.  
  37.     public static List<T> ReaderToList<T>(IDataReader dr) 
  38.     { 
  39.         using (dr) 
  40.         { 
  41.             List<string> field = new List<string>(dr.FieldCount); 
  42.             for (int i = 0; i < dr.FieldCount; i++) 
  43.             { 
  44.                 field.Add(dr.GetName(i).ToLower()); 
  45.             } 
  46.             List<T> list = new List<T>(); 
  47.             while (dr.Read()) 
  48.             { 
  49.                 T model = Activator.CreateInstance<T>(); 
  50.                 foreach (PropertyInfo property in model.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance)) 
  51.                 { 
  52.                     if (field.Contains(property.Name.ToLower())) 
  53.                     { 
  54.                         if (!IsNullOrDBNull(dr[property.Name])) 
  55.                         { 
  56.                             property.SetValue(model, HackType(dr[property.Name], property.PropertyType), null); 
  57.                         } 
  58.                     } 
  59.                 } 
  60.                 list.Add(model); 
  61.             } 
  62.             return list; 
  63.         } 
  64.     } 
  65.     //这个类对可空类型进行判断转换,要不然会报错 
  66.     private static object HackType(object value, Type conversionType) 
  67.     { 
  68.         if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) 
  69.         { 
  70.             if (value == null
  71.                 return null
  72.  
  73.             System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType); 
  74.             conversionType = nullableConverter.UnderlyingType; 
  75.         } 
  76.         return Convert.ChangeType(value, conversionType); 
  77.     } 
  78.  
  79.     private static bool IsNullOrDBNull(object obj) 
  80.     { 
  81.         return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString())) ? true : false
  82.     } 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作