自定义序列化 ISerializable 的实现
2011-04-15 11:23
375 查看
[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>();
//当实现ISerializable接口时,如果该构造函数不存在,则会引发一个SerializationException异常
//该特性表示,该方法只允许序列化器调
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
protected TestObject(SerializationInfo info, StreamingContext context)
{
#region 如果基类也实现了ISerializable接口,则序列化器会自动调用基类的该构造函数,就不需要本段代码
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
}
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>();
//当实现ISerializable接口时,如果该构造函数不存在,则会引发一个SerializationException异常
//该特性表示,该方法只允许序列化器调
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
protected TestObject(SerializationInfo info, StreamingContext context)
{
#region 如果基类也实现了ISerializable接口,则序列化器会自动调用基类的该构造函数,就不需要本段代码
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
}
相关文章推荐
- C#自定义序列化ISerializable的实现方法
- 序列化 - 实现ISerializable
- MSMQ实现自定义序列化存储
- 关于数据序列化(4)自定义序列化的实现,支持常用集合框架
- Magcodes.WeiChat——自定义CustomCreationConverter之实现微信自定义菜单的序列化
- .NET 插件系统框架设计(二) 使用对象序列化实现自定义配置文件管理
- 使用目前hadoop内置的序列化类(不使用自定义序列化类),实现流量统计的功能
- Gson 优雅实现多个枚举的自定义(反)序列化过程
- 【JDK源码分析】04-使用Externalizable实现自定义序列化
- 建议57:实现ISerializable的子类型应负责父类的序列化
- MSMQ实现自定义序列化存储
- 自定义TypeAdapter实现字段为null时序列化为“”空字符串
- Magcodes.WeiChat——自定义CustomCreationConverter之实现微信自定义菜单的序列化
- MSMQ实现自定义序列化存储
- IDictionary 序列化一定要实现父类实现了ISerializable接口的,子类也必须有序列化构造函数,否则反序列化时会出错。
- 编写高质量代码改善C#程序的157个建议——建议57:实现ISerializable的子类型应负责父类的序列化
- 用shelve模块实现Python自定义对象序列化
- 自定义序列化技术2:变长数组的实现
- QDataStream实现自定义对象序列化
- 使用css打造自定义select(非模拟)实现原理及样式