前段時(shí)間做了一個(gè)練手的小項(xiàng)目,名叫Book_Bar,用來(lái)賣書的,采用的是三層架構(gòu),也就是Models,IDAL,DAL,BLL 和 Web , 在DAL層中各個(gè)類中有一個(gè)方法比較常用,那就是RowToClass ,顧名思義,也就是將DataTable 中的數(shù)據(jù)封裝到Models 中。結(jié)果導(dǎo)致在DAL各個(gè)類中寫了很多類似的方法,后來(lái)就直接把它抽取出來(lái)做成了DataTable和DataRow的擴(kuò)展方法,下面是代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespace DAL { /// <summary> /// 用于給 DataTable和 DataRow擴(kuò)展方法 /// </summary> public static class TableExtensionMethod { /// <summary> /// 功能: /// 給DataTable擴(kuò)展了一個(gè)方法,能夠?qū)ataTable中的行轉(zhuǎn)變?yōu)閷?duì)應(yīng)的class對(duì)象,并封裝到List集合中; /// </summary> /// <typeparam name="T">需要轉(zhuǎn)變成為的class類型</typeparam> /// <param name="table">傳入的DataTable對(duì)象</param> /// <returns>返回一個(gè)封裝了對(duì)應(yīng)class的List集合</returns> public static List<T> TableToClass<T>( this DataTable table) { Type type = typeof (T); PropertyInfo[] propArr = type.GetProperties(); //獲取所有屬性 List<T> list = new List<T>(); DataRowCollection rows = table.Rows; int len = rows[0].ItemArray.Length; //獲取第一行的列數(shù),即class的屬性個(gè)數(shù) for ( int i = 0; i < rows.Count; i++) { T t = (T)Activator.CreateInstance(type); for ( int j = 0; j < len; j++) //這里之所以不使用propArr.Length,是因?yàn)橛行㎝odels的屬性在數(shù)據(jù)表中不存在對(duì)應(yīng)的列 { propArr[j].SetValue(t, rows[i][j]); } list.Add(t); t = default (T); } return list; } /// <summary> /// 功能: /// DataRow的擴(kuò)展方法; /// 能夠?qū)ataRow對(duì)象封裝到泛型對(duì)象中 /// </summary> /// <typeparam name="T">需要轉(zhuǎn)換成為的class類型</typeparam> /// <param name="row">被轉(zhuǎn)換的行</param> /// <returns>封裝了行數(shù)據(jù)的class對(duì)象</returns> public static T RowToClass<T>( this DataRow row) { //Type type = Assembly.Load(classFullName).GetType(); Type type = typeof (T); T t = (T)Activator.CreateInstance(type); PropertyInfo[] propArr = type.GetProperties(); int len = row.ItemArray.Length; for ( int i = 0; i < len; i++) { propArr[i].SetValue(t, row[i]); } return t; } /// <summary> /// 功能: /// DataRowCollection的擴(kuò)展方法; /// 能夠?qū)ataRowCollection對(duì)象封裝到泛型List集合中 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="rows"></param> /// <returns></returns> public static List<T> RowToClass<T>( this DataRow row, DataRow[] rowArr) { Type type = typeof (T); PropertyInfo[] propArr = type.GetProperties(); int len = rowArr[0].ItemArray.Length; //獲取數(shù)據(jù)表第一行的列數(shù),即屬性個(gè)數(shù) List<T> list = new List<T>(); for ( int i = 0; i < rowArr.Length; i++) { T t = (T)Activator.CreateInstance(type); for ( int j = 0; j < len; j++) { propArr[j].SetValue(t, rowArr[i][j]); } list.Add(t); t = default (T); } return list; } } } |
上面用到了泛型,反射,擴(kuò)展方法。
之前在使用這行代碼時(shí)出了點(diǎn)小問(wèn)題:
propArr[i].SetValue(t, row[i]);
報(bào)了一個(gè)類型轉(zhuǎn)換異常,斷點(diǎn)調(diào)試之后發(fā)現(xiàn)是因?yàn)?Models 中的屬性的排列和數(shù)據(jù)表的列的順序不一樣導(dǎo)致的,參照數(shù)據(jù)表中字段的順序修改過(guò)來(lái)就好,還有一點(diǎn)就是在循環(huán)對(duì)屬性進(jìn)行賦值時(shí),我選用的是數(shù)據(jù)表中列的個(gè)數(shù),而不是屬性的個(gè)數(shù),(也就是代碼中這里之所以不使用propArr.Length,是因?yàn)橛行㎝odels的屬性在數(shù)據(jù)表中不存在對(duì)應(yīng)的列
)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/daimajun/p/6818069.html