2011-04-16 70 views
1

我有這三種類型的,例如:C#反序列化訂單

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)); 
    } 
} 

在一個第一類型對象的反序列化時間,例如在第一一個TYPE3對象被反序列化,然後 TYPE1被desrialized然後類型2。我需要這門學科: 起初TYPE1 desrialize,然後鍵入2,然後鍵入3 我該怎麼辦呢? 腳註:這不是我的代碼,我不測試,但我的代碼是這樣的。由於其體積的我不鍋在我的帖子...

+0

爲什麼反序列化的事情的順序? – taylonr 2011-04-16 15:13:46

+0

由於恢復一些親子關係... – qiback 2011-04-16 15:15:15

回答

0

這聽起來就好像你需要創建一個整體的序列化的類,其中要求你控制順序的序列化,像這樣:

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... 
} 

領域的其餘部分將被序列化也......覺得周圍這樣的包裝,以保持所要求的一致性..