簡體   English   中英

C#反序列化命令

[英]C# Deserialization Order

我有這三種類型,例如:

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對象反序列化時,例如,首先對類型3對象進行反序列化,然后對類型1進行反序列化,然后對類型2進行反序列化。 我需要這種紀律:首先對type1進行反序列化,然后對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...
}

其余字段也將被序列化...考慮一下它周圍的包裝器,以保持所需的一致性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM