现在的位置: 首页 > 综合 > 正文

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

2012年02月29日 ⁄ 综合 ⁄ 共 5851字 ⁄ 字号 评论关闭

常来博客园, 可一直对它没什么贡献. 真是有些过意不过, 今天下午有些时间, 把一个想法写出来, 大虾就免看了, 为新手提供点思路.
不说费话了, 下面听我道来

电子商务站点, 在一个完整的预订流程中, 用户的一些选择信息需要从一个页面传到下一个页面. 这是特别常用的需求. 一般的做法把这些信息一一的写进隐含域<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大名鼎鼎的反射技术.

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

实体的基类

  1using System;
  2using System.Reflection;
  3
  4namespace 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

一般的实体类.

  1using System;
  2
  3namespace 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

抱歉!评论已关闭.