C#反序列化顺序
例如,我有以下三种类型:C#反序列化顺序,c#,serialization,C#,Serialization,例如,我有以下三种类型: public class Type1 : ISerializable { public List<Type2> field2 { set; get; } public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("field2", field2, typeof (List<Type2&g
public class Type1 : ISerializable
{
public List<Type2> field2 { set; get; }
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("field2", field2, typeof (List<Type2>));
}
protected Type1(SerializationInfo info, StreamingContext context)
{
this.field2 = (List<Type2>) info.GetValue("field2", typeof (List<Type2>));
}
}
public class Type2 : ISerializable
{
public List<Type3> field3 { set; get; }
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("field3", field3, typeof (List<Type3>));
}
protected Type2(SerializationInfo info, StreamingContext context)
{
this.field3 = (List<Type3>) info.GetValue("field3", typeof (Type3));
}
}
public class Type3 : ISerializable
{
public string field;
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("field", field, typeof (string));
}
protected Type3(SerializationInfo info, StreamingContext context)
{
this.field = (string) info.GetValue("field", typeof (string));
}
}
公共类类型1:ISerializable
{
公共列表字段2{set;get;}
public void GetObjectData(SerializationInfo信息、StreamingContext上下文)
{
信息附加值(“字段2”,字段2,类型(列表));
}
受保护的类型1(SerializationInfo信息、StreamingContext上下文)
{
this.field2=(List)info.GetValue(“field2”,typeof(List));
}
}
公共类类型2:ISerializable
{
公共列表字段3{set;get;}
public void GetObjectData(SerializationInfo信息、StreamingContext上下文)
{
信息附加值(“字段3”,字段3,类型(列表));
}
受保护的类型2(SerializationInfo信息、StreamingContext上下文)
{
this.field3=(List)info.GetValue(“field3”,typeof(Type3));
}
}
公共类类型3:ISerializable
{
公共字符串字段;
public void GetObjectData(SerializationInfo信息、StreamingContext上下文)
{
info.AddValue(“字段”,字段,类型(字符串));
}
受保护的类型3(SerializationInfo信息、StreamingContext上下文)
{
this.field=(string)info.GetValue(“field”,typeof(string));
}
}
例如,在Type1对象的反序列化时,首先反序列化type3对象,然后
类型1被去序列化,然后是类型2。我需要这个纪律:
首先进行1型去唾液酸化,然后进行2型去唾液酸化,然后进行3型去唾液酸化。
我怎么做?
脚注:这不是我的代码,我不测试它,但我的代码就是这样。由于它的体积,我不会在我的帖子中添加它…这听起来像是需要创建一个整体可序列化类,在这个类中,您可以根据需要控制序列化的顺序,如下所示:
public class TypePrimary : ISerializable{
private Type1 _type1;
private Type2 _type2;
private Type3 _type3;
protected TypePrimary(Type1 type1, Type2, type2, Type3 type3){
this._type1 = type1;
this._type2 = type2;
this._type3 = type3;
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("type1", this._type1, typeof (Type1));
info.AddValue("type2", this._type2, typeof (Type2));
info.AddValue("type3", this._type3, typeof (Type3));
}
protected TypePrimary(SerializationInfo info, StreamingContext context)
{
this._type1 = (Type1) info.GetValue("type1", typeof (Type1));
this._type2 = (Type2) info.GetValue("type2", typeof (Type2));
this._type3 = (Type3) info.GetValue("type3", typeof (Type3));
}
// Use public getters to return the types...
}
其余字段也将被序列化。。。将其视为一个包装器,以保持所需的一致性。为什么反序列化顺序很重要?因为恢复了一些父子关系。。。