利用反射把数据库查询到的数据转换成Model、List(改良版)

之前也写过一篇这样的博文,但是非常的粗糙。    博文地址

后来看到了一位前辈(@勤快的小熊)对我的博文的评论后,让我看到了更加优雅的实现方式,于是重构了之前的代码。

  1 public static List<T> ConvertToList<T>(DataTable dt)
  2         {
  3             List<T> list = new List<T>(); // 定义集合
  4             Type type = typeof(T); // 获得此模型的类型
  5             string tempName = "";
  6             PropertyInfo[] propertys = type.GetProperties();// 获得此模型的公共属性
  7             foreach (DataRow dr in dt.Rows)
  8             {
  9                 //新建一个模型
 10                 object obj = type.Assembly.CreateInstance(type.FullName);
 11                 foreach (PropertyInfo pi in propertys)
 12                 {
 13                     tempName = pi.Name;
 14                     if (dt.Columns.Contains(tempName))
 15                     {
 16                         if (!pi.CanWrite) continue;
 17                         object value = dr[tempName];
 18                         if (value != DBNull.Value)
 19                             pi.SetValue(obj, value, null);
 20                     }
 21                 }
 22                 list.Add((T)obj);
 23             }
 24             return list;
 25         }
 26 
 27         public static List<T> ConvertToList<T>(IDataReader reader)
 28         {
 29             List<T> list = new List<T>(); // 定义集合
 30             Type type = typeof(T); // 获得此模型的类型
 31             string tempName = "";
 32             PropertyInfo[] propertys = type.GetProperties();// 获得此模型的公共属性
 33             while (reader.Read())
 34             {
 35                 //新建一个模型
 36                 object obj = type.Assembly.CreateInstance(type.FullName);
 37                 foreach (PropertyInfo pi in propertys)
 38                 {
 39                     tempName = pi.Name;
 40                     if (ReaderExists(reader, tempName))
 41                     {
 42                         if (!pi.CanWrite) continue;
 43                         object value = reader[tempName];
 44                         if (value != DBNull.Value)
 45                             pi.SetValue(obj, value, null);
 46                     }
 47                 }
 48                 list.Add((T)obj);
 49             }
 50             return list;
 51         }
 52 
 53         public static T ConvertToModel<T>(IDataReader reader)
 54         {
 55             Type type = typeof(T);
 56             PropertyInfo[] proList = type.GetProperties();
 57             //新建一个模型
 58             object obj = type.Assembly.CreateInstance(type.FullName);
 59             string tempName = "";
 60             if (reader.Read())
 61             {
 62                 foreach (PropertyInfo pi in proList)
 63                 {
 64                     tempName = pi.Name;
 65                     if (ReaderExists(reader, pi.Name))
 66                     {
 67                         if (!pi.CanWrite) continue;
 68                         object value = reader[tempName];
 69                         if (value != DBNull.Value)
 70                             pi.SetValue(obj, value, null);
 71                     }
 72                 }
 73             }
 74             return (T)obj;
 75         }
 76 
 77         public static T ConvertToModel<T>(DataRow row)
 78         {
 79             Type type = typeof(T);
 80             PropertyInfo[] proList = type.GetProperties();
 81             //新建一个模型
 82             object obj = type.Assembly.CreateInstance(type.FullName);
 83             string tempName = "";
 84             foreach(PropertyInfo pi in proList)
 85             {
 86                 tempName = pi.Name;
 87                 if (!string.IsNullOrEmpty(row[tempName].ToString()))
 88                 {
 89                     if (!pi.CanWrite) continue;
 90                     object value = row[tempName];
 91                     if (value != DBNull.Value)
 92                         pi.SetValue(obj, value, null);
 93                 }
 94             }
 95             return (T)obj;
 96         }
 97 
 98         /// <summary>
 99         /// 验证reader是否存在某列
100         /// </summary>
101         /// <param name="reader"></param>
102         /// <param name="columnName"></param>
103         /// <returns></returns>
104         private static bool ReaderExists(IDataReader reader,string columnName)
105         {
106             int count = reader.FieldCount;
107             for(int i = 0; i < count; i++)
108             {
109                 if(reader.GetName(i).Equals(columnName))
110                 {
111                     return true;
112                 }
113             }
114             return false;
115         }
View Code
原文地址:https://www.cnblogs.com/lifuquan/p/5713559.html