您的位置:首页 > 编程语言 > ASP

使用反射技术在asp.net页间传递对象

2008-12-07 16:00 696 查看
电子商务站点, 在一个完整的预订流程中, 用户的一些选择信息需要从一个页面传到下一个页面.
这是特别常用的需求. 一般的做法把这些信息一一的写进隐含域<input type=hidden value="xx"> 再post
到另外一页, 再一一接收. 或者放在Url 中传递.
在asp.net中我们习惯把这用户需求信息, 用一个实体类来表示. 像上面的这种需求, 我们就需要在每一个页面中, 新建实体, 接受参数, 为所有属性设值. 传递到下一页还得一一拿出来写进隐含域.

这样太烦了,程序员是最怕麻烦的. 我们很自然的就会想到下面的这几种办法.

1. 把整个实体写进Session . 这是很简单, 可Session 会过期, 又占用服务器的资源. 明显这做法是不可取的.

2. 用ViewState . viewstate 把值放在html里, 过期是不会.
又不占用服务器的资源. 这看上去不错. 可是ViewState 一般只能在同一页中使用. (当然你可以把你的站点改成使用一个页面实例)
但有一个最大的问题, 把一个对象序列化成字符动不动就上百K 的体积, 让人望而却步.

3. 在实体类中提供ToString()和 BuildObject(string
str)方法, 把所有属性的值加成一个字符串, 用这个字符串来传递. 到了别外一页调用BuildObject方法,
分解字符串,重新还原成对象. 这样可以使一个对象在页之间的传递变得相对容易.
传递的字串符的量不大.还可以使用字符串压缩,无论是放在隐含域中还是Url 中都很方便.

但是第3种方法, 需要为每一个需要在页间传递的对象提供ToString()和BuildObject(string str)方法. 还是老话,程序员是最怕麻烦的.
我们可以再抽象一下. 提供一个基类来做这工作怎么样? 当然不错, 这样的我的这些对象就可以什么都不用做了, 直接继承于这个基类就行了. 可是这个基类怎么写呢? 在基类中,怎么能知道这个类中的所有属性呢? 你一定知道的 .net大名鼎鼎的反射技术.

下面就不用我说什么了, 我把代码放在下面:

实体的基类

1

using System;
2

using System.Reflection;
3


4

namespace WebApplication3
5





{
6



/**//// <summary>
7

/// 所有需要页间传递实体的基类.
8

/// </summary>
9

[Serializable]
10

public class RequestInfo
11





{
12

public RequestInfo()
13





{
14

}
15


16

//把实体的所有属性值传换成字符串
17

public virtual string ToObjectString()
18





{
19

string objString = string.Empty;
20

//得到所有Property
21

PropertyInfo[] Propertys =this.GetType().GetProperties();
22

int i = 0;
23


24

foreach( PropertyInfo pi in Propertys )
25





{
26

object oj = pi.GetValue(this,null);
27

Type type = pi.PropertyType;
28

string valueStr = string.Empty;
29


30

if (oj != null && oj.ToString() !=string.Empty)
31





{
32

if( type == Type.GetType("System.DateTime") )
33





{
34

valueStr = ((DateTime)oj).ToShortDateString();
35

}
36

else
37





{
38

valueStr = oj.ToString();
39

}
40


41

objString += "|" + i.ToString() + "*" + valueStr;
42


43

}
44

i++;
45

}
46

objString = System.Web.HttpUtility.UrlEncode(objString);
47


48

return objString;
49

}
50


51


52

//把字符串还原成对象
53

public virtual RequestInfo BuildObject(string objString)
54





{
55


56


57

objString = System.Web.HttpUtility.UrlDecode(objString);
58


59

object newObject = Activator.CreateInstance( this.GetType() );
60


61

PropertyInfo[] Propertys = newObject.GetType().GetProperties();
62

int i = 0;
63

string [] propertyValue = new string[Propertys.Length];
64

string[] valueArray = objString.Split('|');
65


66

//分解值.
67

for(int j = 0 ; j< valueArray.Length; j ++ )
68





{
69

if (valueArray[j] != null )
70





{
71

int order = 0;
72

string[] valuArray = valueArray[j].Split('*');
73

if (valuArray != null)
74





{
75

if(valuArray[0] != null && valuArray[0]!=string.Empty)
76





{
77

string key = valuArray[0].ToString();
78

string value = valuArray[1].ToString();
79

if (key != string.Empty)
80





{
81

order = int.Parse(key);
82

}
83

propertyValue[order] = value;
84

}
85

}
86

}
87

}
88

//把值置进去.
89

foreach( PropertyInfo pi in Propertys )
90





{
91

if (propertyValue[i] != null)
92





{
93

if (pi.CanWrite)
94





{
95

object obb = propertyValue[i];
96

Type type = pi.PropertyType;
97

obb = Convert.ChangeType(obb, type);
98

pi.SetValue(newObject,obb,null);
99


100

}
101

}
102

else
103





{
104

if (pi.CanWrite)
105





{
106

pi.SetValue(newObject,null,null);
107

}
108

}
109


110

i++;
111

}
112

return (RequestInfo) newObject;
113


114

}
115

}
116

}
117


一般的实体类.

1

using System;
2


3

namespace WebApplication3
4





{
5



/**//// <summary>
6

/// Summary description for HotelRequestInfo.
7

/// </summary>
8

///
9

[Serializable]
10

public class HotelRequestInfo : RequestInfo
11





{
12

private string hotelName;
13

private string hotelID;
14

private string cityName;
15

private int personNum;
16

private int roomNum;
17

private int star;
18


19


20

private DateTime checkInDate;
21

private DateTime checkOutDate;
22


23

public HotelRequestInfo()
24





{
25


26


27

}
28


29

public string HotelID
30





{
31

get
32





{
33

return hotelID;
34

}
35

set
36





{
37

hotelID = value;
38

}
39

}
40


41


42


43

public string HotelName
44





{
45

get
46





{
47

return hotelName;
48

}
49

set
50





{
51

hotelName = value;
52

}
53

}
54


55


56

public string CityName
57





{
58

get
59





{
60

return cityName;
61

}
62

set
63





{
64

cityName = value;
65

}
66

}
67


68

public int Star
69





{
70

get
71





{
72

return star;
73

}
74

set
75





{
76

star = value;
77

}
78

}
79


80

public int PersonNum
81





{
82

get
83





{
84

return personNum;
85

}
86

set
87





{
88

personNum = value;
89

}
90

}
91


92

public int RoomNum
93





{
94

get
95





{
96

return roomNum;
97

}
98

set
99





{
100

roomNum = value;
101

}
102

}
103


104

public DateTime CheckInDate
105





{
106

get
107





{
108

return checkInDate;
109


110

}
111

set
112





{
113


114

checkInDate = value;
115


116

}
117

}
118


119


120

public DateTime CheckOutDate
121





{
122

get
123





{
124

return checkOutDate;
125

}
126

set
127





{
128


129

checkOutDate = value;
130


131

}
132

}
133


134


135


136

}
137

}
138


使用的时候.

1

HotelRequestInfo myInfo = new HotelRequestInfo();
2

myInfo.HotelName = Request["hotelName"].ToString();
3

myInfo.HotelID = Request["hotelId"].ToString();
4

myInfo.Star = Request["star"].ToString();


7

string pValue = myInfo.ToObjectString();

页面中写:

<a href="WebForm5.aspx?pv=<% =pValue%>">下一页</a>
下一页中:

1

HotelRequestInfo myInfo = new HotelRequestInfo() ;
2


3

string a = Request.Params["pv"].ToString();
4


5

myInfo = (HotelRequestInfo)myInfo.BuildObject(a);
6


7

Response.Write("<h1>第五页</h1>");
8

Response.Write(myInfo.HotelName);
9

Response.Write("<hr size=1>");
10

Response.Write(myInfo.HotelID);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: