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

电子商务站点, 在一个完整的预订流程中, 用户的一些选择信息需要从一个页面传到下一个页面. 这是特别常用的需求. 一般的做法把这些信息一一的写进隐含域<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 None.gifusingSystem;
2None.gifusingSystem.Reflection;
3None.gif
4None.gifnamespaceWebApplication3
5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
6ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
7InBlock.gif///所有需要页间传递实体的基类.
8ExpandedSubBlockEnd.gif///</summary>

9InBlock.gif[Serializable]
10InBlock.gifpublicclassRequestInfo
11ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
12InBlock.gifpublicRequestInfo()
13ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
14ExpandedSubBlockEnd.gif}

15InBlock.gif
16InBlock.gif//把实体的所有属性值传换成字符串
17InBlock.gifpublicvirtualstringToObjectString()
18ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
19InBlock.gifstringobjString=string.Empty;
20InBlock.gif//得到所有Property
21InBlock.gifPropertyInfo[]Propertys=this.GetType().GetProperties();
22InBlock.gifinti=0;
23InBlock.gif
24InBlock.gifforeach(PropertyInfopiinPropertys)
25ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
26InBlock.gifobjectoj=pi.GetValue(this,null);
27InBlock.gifTypetype=pi.PropertyType;
28InBlock.gifstringvalueStr=string.Empty;
29InBlock.gif
30InBlock.gifif(oj!=null&&oj.ToString()!=string.Empty)
31ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
32InBlock.gifif(type==Type.GetType("System.DateTime"))
33ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
34InBlock.gifvalueStr=((DateTime)oj).ToShortDateString();
35ExpandedSubBlockEnd.gif}

36InBlock.gifelse
37ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
38InBlock.gifvalueStr=oj.ToString();
39ExpandedSubBlockEnd.gif}

40InBlock.gif
41InBlock.gifobjString+="|"+i.ToString()+"*"+valueStr;
42InBlock.gif
43ExpandedSubBlockEnd.gif}

44InBlock.gifi++;
45ExpandedSubBlockEnd.gif}

46InBlock.gifobjString=System.Web.HttpUtility.UrlEncode(objString);
47InBlock.gif
48InBlock.gifreturnobjString;
49ExpandedSubBlockEnd.gif}

50InBlock.gif
51InBlock.gif
52InBlock.gif//把字符串还原成对象
53InBlock.gifpublicvirtualRequestInfoBuildObject(stringobjString)
54ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
55InBlock.gif
56InBlock.gif
57InBlock.gifobjString=System.Web.HttpUtility.UrlDecode(objString);
58InBlock.gif
59InBlock.gifobjectnewObject=Activator.CreateInstance(this.GetType());
60InBlock.gif
61InBlock.gifPropertyInfo[]Propertys=newObject.GetType().GetProperties();
62InBlock.gifinti=0;
63InBlock.gifstring[]propertyValue=newstring[Propertys.Length];
64InBlock.gifstring[]valueArray=objString.Split('|');
65InBlock.gif
66InBlock.gif//分解值.
67InBlock.giffor(intj=0;j<valueArray.Length;j++)
68ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
69InBlock.gifif(valueArray[j]!=null)
70ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
71InBlock.gifintorder=0;
72InBlock.gifstring[]valuArray=valueArray[j].Split('*');
73InBlock.gifif(valuArray!=null)
74ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
75InBlock.gifif(valuArray[0]!=null&&valuArray[0]!=string.Empty)
76ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
77InBlock.gifstringkey=valuArray[0].ToString();
78InBlock.gifstringvalue=valuArray[1].ToString();
79InBlock.gifif(key!=string.Empty)
80ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
81InBlock.giforder=int.Parse(key);
82ExpandedSubBlockEnd.gif}

83InBlock.gifpropertyValue[order]=value;
84ExpandedSubBlockEnd.gif}

85ExpandedSubBlockEnd.gif}

86ExpandedSubBlockEnd.gif}

87ExpandedSubBlockEnd.gif}

88InBlock.gif//把值置进去.
89InBlock.gifforeach(PropertyInfopiinPropertys)
90ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
91InBlock.gifif(propertyValue[i]!=null)
92ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
93InBlock.gifif(pi.CanWrite)
94ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
95InBlock.gifobjectobb=propertyValue[i];
96InBlock.gifTypetype=pi.PropertyType;
97InBlock.gifobb=Convert.ChangeType(obb,type);
98InBlock.gifpi.SetValue(newObject,obb,null);
99InBlock.gif
100ExpandedSubBlockEnd.gif}

101ExpandedSubBlockEnd.gif}

102InBlock.gifelse
103ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
104InBlock.gifif(pi.CanWrite)
105ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
106InBlock.gifpi.SetValue(newObject,null,null);
107ExpandedSubBlockEnd.gif}

108ExpandedSubBlockEnd.gif}

109InBlock.gif
110InBlock.gifi++;
111ExpandedSubBlockEnd.gif}

112InBlock.gifreturn(RequestInfo)newObject;
113InBlock.gif
114ExpandedSubBlockEnd.gif}

115ExpandedSubBlockEnd.gif}

116ExpandedBlockEnd.gif}

117None.gif

一般的实体类.

1 None.gifusingSystem;
2None.gif
3None.gifnamespaceWebApplication3
4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
5ExpandedSubBlockStart.gifContractedSubBlock.gif/**////<summary>
6InBlock.gif///SummarydescriptionforHotelRequestInfo.
7InBlock.gif///</summary>
8ExpandedSubBlockEnd.gif///

9InBlock.gif[Serializable]
10InBlock.gifpublicclassHotelRequestInfo:RequestInfo
11ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
12InBlock.gifprivatestringhotelName;
13InBlock.gifprivatestringhotelID;
14InBlock.gifprivatestringcityName;
15InBlock.gifprivateintpersonNum;
16InBlock.gifprivateintroomNum;
17InBlock.gifprivateintstar;
18InBlock.gif
19InBlock.gif
20InBlock.gifprivateDateTimecheckInDate;
21InBlock.gifprivateDateTimecheckOutDate;
22InBlock.gif
23InBlock.gifpublicHotelRequestInfo()
24ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
25InBlock.gif
26InBlock.gif
27ExpandedSubBlockEnd.gif}

28InBlock.gif
29InBlock.gifpublicstringHotelID
30ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
31InBlock.gifget
32ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
33InBlock.gifreturnhotelID;
34ExpandedSubBlockEnd.gif}

35InBlock.gifset
36ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
37InBlock.gifhotelID=value;
38ExpandedSubBlockEnd.gif}

39ExpandedSubBlockEnd.gif}

40InBlock.gif
41InBlock.gif
42InBlock.gif
43InBlock.gifpublicstringHotelName
44ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
45InBlock.gifget
46ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
47InBlock.gifreturnhotelName;
48ExpandedSubBlockEnd.gif}

49InBlock.gifset
50ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
51InBlock.gifhotelName=value;
52ExpandedSubBlockEnd.gif}

53ExpandedSubBlockEnd.gif}

54InBlock.gif
55InBlock.gif
56InBlock.gifpublicstringCityName
57ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
58InBlock.gifget
59ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
60InBlock.gifreturncityName;
61ExpandedSubBlockEnd.gif}

62InBlock.gifset
63ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
64InBlock.gifcityName=value;
65ExpandedSubBlockEnd.gif}

66ExpandedSubBlockEnd.gif}

67InBlock.gif
68InBlock.gifpublicintStar
69ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
70InBlock.gifget
71ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
72InBlock.gifreturnstar;
73ExpandedSubBlockEnd.gif}

74InBlock.gifset
75ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
76InBlock.gifstar=value;
77ExpandedSubBlockEnd.gif}

78ExpandedSubBlockEnd.gif}

79InBlock.gif
80InBlock.gifpublicintPersonNum
81ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
82InBlock.gifget
83ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
84InBlock.gifreturnpersonNum;
85ExpandedSubBlockEnd.gif}

86InBlock.gifset
87ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
88InBlock.gifpersonNum=value;
89ExpandedSubBlockEnd.gif}

90ExpandedSubBlockEnd.gif}

91InBlock.gif
92InBlock.gifpublicintRoomNum
93ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
94InBlock.gifget
95ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
96InBlock.gifreturnroomNum;
97ExpandedSubBlockEnd.gif}

98InBlock.gifset
99ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
100InBlock.gifroomNum=value;
101ExpandedSubBlockEnd.gif}

102ExpandedSubBlockEnd.gif}

103InBlock.gif
104InBlock.gifpublicDateTimeCheckInDate
105ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
106InBlock.gifget
107ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
108InBlock.gifreturncheckInDate;
109InBlock.gif
110ExpandedSubBlockEnd.gif}

111InBlock.gifset
112ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
113InBlock.gif
114InBlock.gifcheckInDate=value;
115InBlock.gif
116ExpandedSubBlockEnd.gif}

117ExpandedSubBlockEnd.gif}

118InBlock.gif
119InBlock.gif
120InBlock.gifpublicDateTimeCheckOutDate
121ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
122InBlock.gifget
123ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
124InBlock.gifreturncheckOutDate;
125ExpandedSubBlockEnd.gif}

126InBlock.gifset
127ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
128InBlock.gif
129InBlock.gifcheckOutDate=value;
130InBlock.gif
131ExpandedSubBlockEnd.gif}

132ExpandedSubBlockEnd.gif}

133InBlock.gif
134InBlock.gif
135InBlock.gif
136ExpandedSubBlockEnd.gif}

137ExpandedBlockEnd.gif}

138None.gif

使用的时候.

1 None.gifHotelRequestInfomyInfo=newHotelRequestInfo();
2None.gifmyInfo.HotelName=Request["hotelName"].ToString();
3None.gifmyInfo.HotelID=Request["hotelId"].ToString();
4None.gifmyInfo.Star=Request["star"].ToString();None.gif
7None.gifstringpValue=myInfo.ToObjectString();

页面中写:

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

下一页中:

1 None.gif HotelRequestInfomyInfo = new HotelRequestInfo();
2 None.gif
3 None.gif string a = Request.Params[ " pv " ].ToString();
4 None.gif
5 None.gifmyInfo = (HotelRequestInfo)myInfo.BuildObject(a);
6 None.gif
7 None.gifResponse.Write( " <h1>第五页</h1> " );
8 None.gifResponse.Write(myInfo.HotelName);
9 None.gifResponse.Write( " <hrsize=1> " );
10 None.gifResponse.Write(myInfo.HotelID);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值