C#自定義序列化ISerializable的實(shí)現(xiàn)方法
更新時(shí)間:2015年04月28日 10:58:35 作者:igoo
這篇文章主要介紹了C#自定義序列化ISerializable的實(shí)現(xiàn)方法,涉及C#序列化的操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#自定義序列化ISerializable的實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
[Serializable]
public class BaseObject
{
[OptionalField]
private string _str = "I am BaseObject";
}
[Serializable]
public class TestObject : BaseObject, ISerializable
{
int a;
string strName = "";
Color c = Color.Red;
DataTable _dtColors = null;
[OptionalField]
ArrayList list = new ArrayList();
[OptionalField]
List<int> list1 = new List<int>();
[OptionalField]
Dictionary<int, string> dic = new Dictionary<int, string>();
//當(dāng)實(shí)現(xiàn)ISerializable接口時(shí),如果該構(gòu)造函數(shù)不存在,則會(huì)引發(fā)一個(gè)SerializationException異常
//該特性表示,該方法只允許序列化器調(diào)
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
protected TestObject(SerializationInfo info, StreamingContext context)
{
#region 如果基類也實(shí)現(xiàn)了ISerializable接口,則序列化器會(huì)自動(dòng)調(diào)用基類的該構(gòu)造函數(shù),就不需要本段代碼
Type basetype = this.GetType().BaseType;
MemberInfo[] mi = FormatterServices.GetSerializableMembers(basetype, context);
for (int i = 0; i < mi.Length; i++)
{
//由于AddValue不能添加重名值,為了避免子類變量名與基類變量名相同,將基類序列化的變量名加上基類類名
FieldInfo fi = (FieldInfo)mi[0];
object objValue = info.GetValue(basetype.FullName + "+" + fi.Name, fi.FieldType);
fi.SetValue(this, objValue);
}
#endregion
a = info.GetInt32("a");
strName = info.GetString("strName");
c = (Color)info.GetValue("c", typeof(Color));
_dtColors = (DataTable)info.GetValue("_dtColors", typeof(DataTable));
list = (ArrayList)info.GetValue("list", typeof(ArrayList));
list1 = (List<int>)info.GetValue("list1", typeof(List<int>));
dic = (Dictionary<int, string>)info.GetValue("dic", typeof(Dictionary<int, string>));
}
public TestObject()
{
a = 100;
strName = "daps";
InitColorTable();
list1.Add(10);
list1.Add(20);
}
#region ISerializable 成員
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter =true)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("a", a);
info.AddValue("strName", strName);
info.AddValue("c", c);
info.AddValue("_dtColors", _dtColors);
info.AddValue("list", list);
info.AddValue("list1", list1);
info.AddValue("dic", dic);
Type basetype = this.GetType().BaseType;
MemberInfo[] mi = FormatterServices.GetSerializableMembers(basetype, context);
for (int i = 0; i < mi.Length; i++)
{
//由于AddValue不能添加重名值,為了避免子類變量名與基類變量名相同,將基類序列化的變量名加上基類類名
info.AddValue(basetype.FullName + "+" + mi[i].Name, ((FieldInfo)mi[i]).GetValue(this));
}
}
#endregion
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#簡易人機(jī)對(duì)抗“石頭剪刀布”游戲的實(shí)現(xiàn)
本文主要介紹了C#簡易人機(jī)對(duì)抗“石頭剪刀布”游戲的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
用C#獲取硬盤序列號(hào),CPU序列號(hào),網(wǎng)卡MAC地址的源碼
用C#獲取硬盤序列號(hào),CPU序列號(hào),網(wǎng)卡MAC地址的源碼...2007-03-03
C#實(shí)現(xiàn)根據(jù)銀行卡卡號(hào)判斷銀行名
這篇文章主要介紹了C#實(shí)現(xiàn)根據(jù)銀行卡卡號(hào)判斷銀行名,是從其他網(wǎng)友的java程序改編而來,有需要的小伙伴可以參考下。2015-07-07
C# WinForm實(shí)現(xiàn)自動(dòng)更新程序的方法詳解
這一篇就著重寫一下客戶端的代碼,客戶端主要實(shí)現(xiàn)的有:啟動(dòng)后檢測(cè)本地的xml文件,然后發(fā)送到服務(wù)器獲取需要更新的文件以及版本列表,感興趣的小伙伴可以了解一下2022-10-10
c# winform異步不卡界面的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于c# winform異步不卡界面的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

