菜鳥一枚,入園已有兩年三個月,這還是第一次寫博客,請各位大神斧正。 這是我寫的一個工具類,通常我們從資料庫查詢到一個 DataReader 或者是 一個 Table , 想要轉換成 一個 list 或者是 一個model 的話 , 一般情況下是使用foreach 迴圈reader或是table的ro ...
菜鳥一枚,入園已有兩年三個月,這還是第一次寫博客,請各位大神斧正。
這是我寫的一個工具類,通常我們從資料庫查詢到一個 DataReader 或者是 一個 Table , 想要轉換成 一個 list 或者是 一個model 的話 , 一般情況下是使用foreach 迴圈reader或是table的rows,然後在迴圈內創建個對象,通過reader[“列名”]來賦值對象的屬性。如果表的欄位少的話,用這種方式還可以,速度也快一點。但是如果後續還會增加欄位的話,那就非常麻煩了,要改很多地方。這工作量太大了,而且還很容易出錯。所以這個時候使用反射來轉換的話就非常便捷了。我們只管增加欄位,改一下數據表對應的model,調用這個工具類的方法,傳入相對應的參數就能得到想要的結果。
誒,文筆不好,直接上代碼吧。
1 /// <summary> 2 /// table轉list 3 /// </summary> 4 /// <typeparam name="T">list類型</typeparam> 5 /// <param name="obj">model對象</param> 6 /// <param name="table">數據源table</param> 7 /// <returns>list</returns> 8 public static List<T> FillListByDataTable<T>(T obj, DataTable table) 9 { 10 11 Type type = obj.GetType(); 12 13 List<T> list = new List<T>(); 14 15 PropertyInfo[] properties = type.GetProperties();//model對象的屬性集合 16 17 DataColumnCollection dcc = table.Columns;//列集合 18 19 foreach (DataRow row in table.Rows) 20 { 21 //複製一個model對象(深拷貝) 22 MemoryStream stream = new MemoryStream(); 23 BinaryFormatter formatter = new BinaryFormatter(); 24 formatter.Serialize(stream, obj); 25 stream.Position = 0; 26 T newObj = (T)formatter.Deserialize(stream); 27 28 foreach (PropertyInfo p in properties) 29 { 30 Type t = p.PropertyType; 31 32 if (dcc.Contains(p.Name.ToString())) 33 { 34 if (!string.IsNullOrEmpty(row[p.Name].ToString())) 35 { 36 //判斷欄位類型 37 if (t.Name != "Nullable`1") 38 { 39 p.SetValue(newObj, Convert.ChangeType(row[p.Name], t, null)); 40 } 41 else if (t.FullName == "System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 42 { 43 p.SetValue(newObj, Convert.ChangeType(row[p.Name], typeof(DateTime), null)); 44 } 45 else if (t.FullName == "System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 46 { 47 p.SetValue(newObj, Convert.ChangeType(row[p.Name], typeof(DateTime), null)); 48 } 49 else if (t.FullName == "System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 50 { 51 p.SetValue(newObj, Convert.ChangeType(row[p.Name], typeof(Int32), null)); 52 } 53 else if (t.FullName == "System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 54 { 55 p.SetValue(newObj, Convert.ChangeType(row[p.Name], typeof(Int32), null)); 56 } 57 else if (t.FullName == "System.Nullable`1[[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 58 { 59 p.SetValue(newObj, Convert.ChangeType(row[p.Name], typeof(Int64), null)); 60 } 61 else if (t.FullName == "System.Nullable`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 62 { 63 p.SetValue(newObj, Convert.ChangeType(row[p.Name], typeof(Int64), null)); 64 } 65 else if (t.FullName == "System.Nullable`1[[System.Decimal, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 66 { 67 p.SetValue(newObj, Convert.ChangeType(row[p.Name], typeof(Decimal), null)); 68 } 69 else if (t.FullName == "System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 70 { 71 p.SetValue(newObj, Convert.ChangeType(row[p.Name], typeof(Decimal), null)); 72 } 73 else if (t.FullName == "System.Nullable`1[[System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 74 { 75 p.SetValue(newObj, Convert.ChangeType(row[p.Name], typeof(Boolean), null)); 76 } 77 else if (t.FullName == "System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 78 { 79 p.SetValue(newObj, Convert.ChangeType(row[p.Name], typeof(Boolean), null)); 80 } 81 } 82 83 } 84 } 85 list.Add(newObj); 86 } 87 return list; 88 } 89 90 /// <summary> 91 /// reader轉list 92 /// </summary> 93 /// <typeparam name="T">list類型</typeparam> 94 /// <param name="obj">model對象</param> 95 /// <param name="reader">數據源reader</param> 96 /// <returns>list </returns> 97 public static List<T> FillListByDataReader<T>(T obj ,IDataReader reader) 98 { 99 List<T> list = new List<T>(); 100 101 Type t = obj.GetType(); 102 103 PropertyInfo[] proList = t.GetProperties(); 104 105 while(reader.Read()) 106 { 107 MemoryStream stream = new MemoryStream(); 108 BinaryFormatter formatter = new BinaryFormatter(); 109 formatter.Serialize(stream, obj); 110 stream.Position = 0; 111 T newObj = (T)formatter.Deserialize(stream); 112 113 foreach (PropertyInfo pro in proList) 114 { 115 Type tp = pro.PropertyType; 116 117 DataFieldAttribute attr = pro.GetCustomAttributes().Count() > 0 ? pro.GetCustomAttributes().First() as DataFieldAttribute : null; 118 119 if (attr != null && attr.ColumnName == pro.Name) 120 { 121 if (ReaderExists(reader, pro.Name) && !string.IsNullOrEmpty(reader[pro.Name].ToString())) 122 { 123 124 if (tp.Name != "Nullable`1") 125 { 126 pro.SetValue(newObj, Convert.ChangeType(reader[pro.Name], tp)); 127 } 128 else if (tp.FullName == "System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 129 { 130 pro.SetValue(newObj, Convert.ChangeType(reader[pro.Name], typeof(DateTime), null)); 131 } 132 else if (tp.FullName == "System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 133 { 134 pro.SetValue(newObj, Convert.ChangeType(reader[pro.Name], typeof(DateTime), null)); 135 } 136 else if (tp.FullName == "System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 137 { 138 pro.SetValue(newObj, Convert.ChangeType(reader[pro.Name], typeof(Int32), null)); 139 } 140 else if (tp.FullName == "System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 141 { 142 pro.SetValue(newObj, Convert.ChangeType(reader[pro.Name], typeof(Int32), null)); 143 } 144 else if (tp.FullName == "System.Nullable`1[[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 145 { 146 pro.SetValue(newObj, Convert.ChangeType(reader[pro.Name], typeof(Int64), null)); 147 } 148 else if (tp.FullName == "System.Nullable`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 149 { 150 pro.SetValue(newObj, Convert.ChangeType(reader[pro.Name], typeof(Int64), null)); 151 } 152 else if (tp.FullName == "System.Nullable`1[[System.Decimal, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 153 { 154 pro.SetValue(newObj, Convert.ChangeType(reader[pro.Name], typeof(Decimal), null)); 155 } 156 else if (tp.FullName == "System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 157 { 158 pro.SetValue(newObj, Convert.ChangeType(reader[pro.Name], typeof(Decimal), null)); 159 } 160 else if (tp.FullName == "System.Nullable`1[[System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 161 { 162 pro.SetValue(newObj, Convert.ChangeType(reader[pro.Name], typeof(Boolean), null)); 163 } 164 else if (tp.FullName == "System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 165 { 166 pro.SetValue(newObj, Convert.ChangeType(reader[pro.Name], typeof(Boolean), null)); 167 } 168 } 169 } 170 } 171 list.Add(newObj); 172 } 173 174 return list; 175 } 176 177 /// <summary> 178 /// reader轉model對象 179 /// </summary> 180 /// <typeparam name="T">類型</typeparam> 181 /// <param name="obj">model對象</param> 182 /// <param name="reader">數據源reader</param> 183 /// <returns>model對象</returns> 184 public static T FillObjectByDataReader<T>(T obj, IDataReader reader) 185 { 186 Type t = obj.GetType(); 187 188 PropertyInfo[] proList = t.GetProperties(); 189 190 if (reader.Read()) 191 { 192 foreach (PropertyInfo pro in proList) 193 { 194 Type tp = pro.PropertyType; 195 196 DataFieldAttribute attr = pro.GetCustomAttributes().Count() > 0 ? pro.GetCustomAttributes().First() as DataFieldAttribute : null; 197 198 if (attr != null && attr.ColumnName == pro.Name) 199 { 200 if (ReaderExists(reader, pro.Name) && !string.IsNullOrEmpty(reader[pro.Name].ToString())) 201 { 202 if (tp.Name != "Nullable`1") 203 { 204 pro.SetValue(obj, Convert.ChangeType(reader[pro.Name], tp)); 205 } 206 else if (tp.FullName == "System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 207 { 208 pro.SetValue(obj, Convert.ChangeType(reader[pro.Name], typeof(DateTime), null)); 209 } 210 else if (tp.FullName == "System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 211 { 212 pro.SetValue(obj, Convert.ChangeType(reader[pro.Name], typeof(DateTime), null)); 213 } 214 else if (tp.FullName == "System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 215 { 216 pro.SetValue(obj, Convert.ChangeType(reader[pro.Name], typeof(Int32), null)); 217 } 218 else if (tp.FullName == "System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 219 { 220 pro.SetValue(obj, Convert.ChangeType(reader[pro.Name], typeof(Int32), null)); 221 } 222 else if (tp.FullName == "System.Nullable`1[[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 223 { 224 pro.SetValue(obj, Convert.ChangeType(reader[pro.Name], typeof(Int64), null)); 225 } 226 else if (tp.FullName == "System.Nullable`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 227 { 228 pro.SetValue(obj, Convert.ChangeType(reader[pro.Name], typeof(Int64), null)); 229 } 230 else if (tp.FullName == "System.Nullable`1[[System.Decimal, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 231 { 232 pro.SetValue(obj, Convert.ChangeType(reader[pro.Name], typeof(Decimal), null)); 233 } 234 else if (tp.FullName == "System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 235 { 236 pro.SetValue(obj, Convert.ChangeType(reader[pro.Name], typeof(Decimal), null)); 237 } 238 else if (tp.FullName == "System.Nullable`1[[System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 239 { 240 pro.SetValue(obj, Convert.ChangeType(reader[pro.Name], typeof(Boolean), null)); 241 } 242 else if (tp.FullName == "System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 243 { 244 pro.SetValue(obj, Convert.ChangeType(reader[pro.Name], typeof(Boolean), null)); 245 } 246 } 247 } 248 } 249 } 250 251 return obj; 252 } 253 254 /// <summary> 255 /// row轉model對象 256 /// </summary> 257 /// <typeparam name="T">model類型</typeparam> 258 /// <param name="obj">model對象</param> 259 /// <param name="row">數據源row</param> 260 /// <returns>model對象</returns> 261 public static T FillObjectByDataTable<T>(T obj, DataRow row) 262 { 263 Type t = obj.GetType(); 264 265 PropertyInfo[] proList = t.GetProperties(); 266 267 foreach (PropertyInfo pro in proList) 268 { 269 Type tp = pro.PropertyType; 270 271 DataFieldAttribute attr = pro.GetCustomAttributes().Count() > 0 ? pro.GetCustomAttributes().First() as DataFieldAttribute : null; 272 273 if (attr != null && attr.ColumnName == pro.Name) 274 { 275 if (!string.IsNullOrEmpty(row[pro.Name].ToString())) 276 { 277 if (tp.Name != "Nullable`1") 278 { 279 pro.SetValue(obj, Convert.ChangeType(row[pro.Name], tp, null)); 280 } 281 else if (tp.FullName == "System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 282 { 283 pro.SetValue(obj, Convert.ChangeType(row[pro.Name], typeof(DateTime), null)); 284 } 285 else if (tp.FullName == "System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 286 { 287 pro.SetValue(obj, Convert.ChangeType(row[pro.Name], typeof(DateTime), null)); 288 } 289 else if (tp.FullName == "System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 290 { 291 pro.SetValue(obj, Convert.ChangeType(row[pro.Name], typeof(Int32), null)); 292 } 293 else if (tp.FullName == "System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 294 { 295 pro.SetValue(obj, Convert.ChangeType(row[pro.Name], typeof(Int32), null)); 296 } 297 else if (tp.FullName == "System.Nullable`1[[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 298 { 299 pro.SetValue(obj, Convert.ChangeType(row[pro.Name], typeof(Int64), null)); 300 } 301 else if (tp.FullName == "System.Nullable`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 302 { 303 pro.SetValue(obj, Convert.ChangeType(row[pro.Name], typeof(Int64), null)); 304 } 305 else if (tp.FullName == "System.Nullable`1[[System.Decimal, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 306 { 307 pro.SetValue(obj, Convert.ChangeType(row[pro.Name], typeof(Decimal), null)); 308 } 309 else if (tp.FullName == "System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 310 { 311 pro.SetValue(obj, Convert.ChangeType(row[pro.Name], typeof(Decimal), null)); 312 } 313 else if (tp.FullName == "System.Nullable`1[[System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 314 { 315 pro.SetValue(obj, Convert.ChangeType(row[pro.Name], typeof(Boolean), null)); 316 } 317 else if (tp.FullName == "System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]") 318 { 319 pro.SetValue(obj, Convert.ChangeType(row[pro.Name], typeof(Boolean), null)); 320 } 321 } 322 323 } 324 } 325 326 327 return obj; 328 } 329 330 /// <summary> 331 /// list轉table 332 /// </summary> 333 /// <typeparam name="T">list類型</typeparam> 334 /// <param name="list">數據源list</param> 335 /// <param name="table">table</param> 336 /// <returns>table</returns> 337 public static DataTable FillDataTableByObject<T>(List<T> list , DataTable table) 338 { 339 if (list.Count > 0) 340 { 341 Type type = list[0].GetType(); 342 343 PropertyInfo[] properties = type.GetProperties(); 344 345 DataColumnCollection dcc = table.Columns; 346 347 foreach (var obj in list) 348 { 349 DataRow row = table.NewRow(); 350 foreach (PropertyInfo p in properties) 351 { 352 if (dcc.Contains(p.Name.ToString())) 353 { 354 row[p.Name] = p.GetValue(obj); 355