程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 利用反射把數據庫查詢到的數據轉換成Model、List(改良版),數據轉換model

利用反射把數據庫查詢到的數據轉換成Model、List(改良版),數據轉換model

編輯:C#入門知識

利用反射把數據庫查詢到的數據轉換成Model、List(改良版),數據轉換model


之前也寫過一篇這樣的博文,但是非常的粗糙。    博文地址

後來看到了一位前輩(@勤快的小熊)對我的博文的評論後,讓我看到了更加優雅的實現方式,於是重構了之前的代碼。

 

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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved