C# 反射之SqlDatareader转换为Model实体.

虽说反射的效率比较低,但是在当今的时代,盛行的大数据,以及高并发的产生,硬件的产能正在逐渐的提升,所以我们可以用空间来换取时间.反射所消耗的那些性能问题其实在企业级开发而言也就无所谓了.二 : 反射得确是可以提升开发效率.

将SqlDataReader反射转换为Model实体,省去了每个Reader还要判断转换一下,如此消耗开发效率的问题,怎么能容忍呢?

代码 : 

 1         /// <summary>
 2         ///   将SqlDataReader转换为Model实体
 3         /// </summary>
 4         /// <typeparam name="T">实例类名</typeparam>
 5         /// <param name="dr">Reader对象</param>
 6         /// <returns>实体对象</returns>
 7         public static T ReaderToModel<T>(IDataReader dr)
 8         {
 9             try
10             {
11                 using (dr)
12                 {
13                     if (dr.Read())
14                     {
15                         Type modelType = typeof(T);
16                         T model = Activator.CreateInstance<T>();
17                         for (int i = 0; i < dr.FieldCount; i++)
18                         {
19                             if(!IsNullOrDbNull(dr[i]))
20                             {
21                                 PropertyInfo pi = modelType.GetProperty(GetPropertyName(dr.GetName(i)));
22                                 pi.SetValue(model, HackType(dr[i], pi.PropertyType), null);
23                             } 
25                         }
26                         return model;
27                     }
28                 }
29                 return default(T);
30             }
31             catch (Exception)
32             {
33 
34                 throw;
35             }
36         } 
38 
39         /// <summary>
40         ///  对可空类型进行判断.
41         /// </summary> 
42         private static object HackType(object value, Type conversionType)
43         {
44             if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
45             {
46                 if (value == null)
47                 {
48                     return null;
49                 }
50                 System.ComponentModel.NullableConverter nullAbleConverter = new System.ComponentModel.NullableConverter(conversionType);
51                 conversionType = nullAbleConverter.UnderlyingType;
52             }
53             return Convert.ChangeType(value, conversionType);
54         }
55 
56         /// <summary>
57         ///  判断字段值是否为NUll
58         /// </summary>
59         /// <param name="obj"></param>
60         /// <returns></returns>
61         private static bool IsNullOrDbNull(object obj)
62         {
63             return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString())) ? true : false;
64         }
65 
66         /// <summary>
67         ///  获取属性类的名称
68         /// </summary>
69         /// <param name="column">列名</param>
70         /// <returns>列名</returns>
71         private static string GetPropertyName(string column)
72         { 
74             string[] narr = column.Split('_');
75             column = "";
76             for (int i = 0; i < narr.Length; i++)
77             {
78                 if (narr[i].Length > 1)
79                 {
80                     column += narr[i].Substring(0, 1).ToUpper() + narr[i].Substring(1);
81                 }
82                 else
83                 {
84                     column += narr[i].Substring(0, 1).ToUpper();
85                 }
86             }
87             return column;
88         }

 

posted @ 2014-11-13 16:57  刘彬  阅读(2019)  评论(0编辑  收藏  举报