对象序列化及反序列化

序列化是指将对象实例的状态存储到存储媒体的过程。在此过程中,先将对象的公共字段和私有字段以及类的名称(包括类所在的程序集)转换为字节流,然后再把字节流写入数据流。在随后对对象进行反序列化时,将创建出与原对象完全相同的副本。

如果需要将对象保存在页面ViewState中,即需要将类实现序列化,将对象从ViewState中取出,需要将对象反序列化。

using  System;
using  System.Runtime.Serialization;
using  System.Runtime.Serialization.Formatters.Binary;

namespace  BTTech.WebModules.WebMail
{
    
/// <summary>
    
/// WebMailAccount 帐号对象。
    
/// </summary>

    [Serializable]
    
public class WebMailAccount: Object ,ISerializable
    
{
        
private int account_id;
        
public int Account_ID
        
{
            
get {return account_id;}
        }

        
private string user_id;
        
public string User_ID
        
{
            
get {return user_id;}
        }

        
private string email;
        
public string Email
        
{
            
get {return email;}
        }

        
private string popserver;
        
public string PopServer
        
{
            
get {return popserver;}
        }

        
private string smtpserver;
        
public string SmtpServer
        
{
            
get {return smtpserver;}
        }

        
private LoginType logintype;
        
public LoginType Logintype
        
{
            
get {return logintype;}
        }

        
private bool needpass;
        
public bool Needpass
        
{
            
get {return needpass;}
        }

        
public WebMailAccount(int account_id, string user_id, string email, string popserver, string smtpserver,
            LoginType logintype, 
bool needpass)
        
{
            
this.account_id = account_id;
            
this.user_id = user_id;
            
this.email = email;
            
this.popserver = popserver;
            
this.smtpserver = smtpserver;
            
this.logintype = logintype;
            
this.needpass = needpass;
        }


        
/// <summary>
        
/// Smtp登陆类型
        
/// </summary>

        public enum LoginType
        
{
            
/// <summary>
            
/// 登陆只需用户名
            
/// </summary>

            UserNameOnly = 1,
            
/// <summary>
            
/// 登陆需要用户名和域名
            
/// </summary>

            UserNameandDomain = 2,
        }

        

        
/// <summary>
        
/// 序列化
        
/// </summary>
        
/// <param name="info"></param>
        
/// <param name="context"></param>

        void  ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        
{
            info.AddValue(
"AccountID",account_id);
            info.AddValue(
"User_ID",user_id);
            info.AddValue(
"Email",email);
            info.AddValue(
"PopServer",popserver);
            info.AddValue(
"SmtpServer",smtpserver);
            info.AddValue(
"LoginType",(int)logintype);
            info.AddValue(
"NeedPass",needpass);
        }


        
/// <summary>
        
/// 反序列化
        
/// </summary>
        
/// <param name="info"></param>
        
/// <param name="context"></param>

        protected WebMailAccount(SerializationInfo info, StreamingContext context)
        
{
            account_id    
= info.GetInt32("AccountID");
            user_id        
= info.GetString("User_ID");
            email        
= info.GetString("Email");
            popserver    
= info.GetString("PopServer");
            smtpserver    
= info.GetString("SmtpServer");
            logintype    
= (LoginType)(info.GetInt32("LoginType"));
            needpass    
= info.GetBoolean("NeedPass");
        }

    }

}

你可能感兴趣的:(反序列化)