2011-04-16 4 views
1

i ont ces trois types, par exemple:C# Désérialisation ordre

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

au moment de la désérialisation d'un objet Type1, par exemple d'abord un objet type3 est désérialisé puis type1 est desrialized puis type2. J'ai besoin de cette discipline: Au premier type1 dessinez, puis tapez 2, puis tapez 3. comment puis-je le faire? note de bas de page: Ce n'est pas mon code et je ne le teste pas, mais mon code est comme ça. en raison de son volume je ne pot dans mon post ...

+0

pourquoi l'ordre de la matière désérialisation? – taylonr

+0

en raison de la récupération d'une relation parent-enfant ... – qiback

Répondre

0

Cela sonne comme si vous devez créer une classe d'ensemble sérialisable où vous contrôler la sérialisation dans l'ordre, au besoin, quelque chose comme ceci:

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

le reste des champs sera publié en feuilleton aussi ... penser à ce wrapper autour d'elle pour maintenir la cohérence qui est nécessaire ..