开发者

C# Deserialization Order

i have these three types for example:

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

at deserialization time of a Type1 object, for example at first 开发者_JAVA百科a type3 object is deserialized and then type1 is desrialized and then Type2. I Need this discipline: At first type1 desrialize, then type 2 and then type 3. how can i do it? footnote: That Is Not My code and i dont test that, but my code is like that. because of its volume i dont pot it in my post...


This sounds like as if you need to create a overall serializable class where you control the serialization in the order as required, something like this:

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

The rest of the fields will be serialized also... think of this a wrapper around it to maintain the consistency that is required..


It looks like there is a built-in feature for specifying the ordering during serialization:

https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-member-order

I think the example is self-explanatory:

[DataContract]
public class BaseType
{

    [DataMember]
    public string zebra;
}
[DataContract]
public class DerivedType : BaseType
{
    [DataMember(Order = 0)]
    public string bird;
    [DataMember(Order = 1)]
    public string parrot;
    [DataMember]
    public string dog;
    [DataMember(Order = 3)]
    public string antelope;
    [DataMember]
    public string cat;
    [DataMember(Order = 1)]
    public string albatross;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜