序列化是指将对象实例的状态存储到存储媒体的过程。在此过程中,先将对象的公共字段和私有字段以及类的名称(包括类所在的程序集)转换为字节流,然后再把字节流写入数据流。在随后对对象进行反序列化时,将创建出与原对象完全相同的副本。
如果需要将对象保存在页面ViewState中,即需要将类实现序列化,将对象从ViewState中取出,需要将对象反序列化。
using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
![](/Images/OutliningIndicators/None.gif)
namespace BTTech.WebModules.WebMail
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// WebMailAccount 帐号对象。
/// </summary>
[Serializable]
public class WebMailAccount: Object ,ISerializable
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private int account_id;
public int Account_ID
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return account_id;}
}
private string user_id;
public string User_ID
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return user_id;}
}
private string email;
public string Email
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return email;}
}
private string popserver;
public string PopServer
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return popserver;}
}
private string smtpserver;
public string SmtpServer
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return smtpserver;}
}
private LoginType logintype;
public LoginType Logintype
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return logintype;}
}
private bool needpass;
public bool Needpass
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return needpass;}
}
public WebMailAccount(int account_id, string user_id, string email, string popserver, string smtpserver,
LoginType logintype, bool needpass)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Smtp登陆类型
/// </summary>
public enum LoginType
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 登陆只需用户名
/// </summary>
UserNameOnly = 1,
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 登陆需要用户名和域名
/// </summary>
UserNameandDomain = 2,
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 序列化
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 反序列化
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
protected WebMailAccount(SerializationInfo info, StreamingContext context)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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");
}
}
}
![](/Images/OutliningIndicators/None.gif)