数组的反序列化总是给人空数组

数组的反序列化总是给人空数组

本文介绍了数组的反序列化总是给人空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我做了序列化/ deseriablizeable与ISerializable的子类自定义的抽象基类。当我做这个类的子类的单个实例序列化/反序列化,一切工作正常。但是,当我做他们的数组我总是落得空对反序列化数组。序列化与BinaryFormatter的完成。

I have a custom abstract base class with sub classes that I've made serializable/deseriablizeable with ISerializable. When I do serialization/deserialization of single instances of this class' sub classes, everything works fine. However, when I do an array of them I always end up with an array of nulls on deserialization. Serialization is done with BinaryFormatter.

中的项目都包含在一个

public ObservableCollection<Trade> Trades { get; private set; }

在系列化这是在做GetObjectData使用上的SerializationInfo参数:

On serialization this is done in GetObjectData on the SerializationInfo parameter:

Trade[] trades = (Trade[])Trades.ToArray<Trade>();
            info.AddValue("trades", trades);

和反序列化上这是在序列化构造函数中完成还取决于SerializationInfo中的参数:

And on deserialization this is done in the serialization constructor also on the SerializationInfo parameter:

Trade[] trades = (Trade[])info.GetValue("trades", typeof(Trade[]));

            foreach (Trade t in trades)
            {
                Trades.Add(t);
            }

反序列化总是给我空的数组,正如我前面提到,单个项目的序列化和deseriaizes这个code就好了:

Deserialization always gives me an array of nulls and as I mentioned earlier, a single item serializes and deseriaizes just fine with this code:

连载(GetObjectData方法):

Serialization (GetObjectData method):

info.AddValue("trade", Trades.First<Trade>());

反序列化(序列化构造函数):

Deserialization (Serialization Constructor):

Trade t = (Trade)info.GetValue("trade", typeof(Trade));
            Trades.Add(t);

这是一个常见的​​问题?我似乎找不到发生其他任何人在它至少运行。希望有一个解决方案:),如果我需要更多的信息/ code供应你刚才告诉我的。

Is this a common problem? I seem to find no occurrences of anyone else running in to it at least. Hopefully there is a solution :) and if I need to supply you with more information/code just tell me.

谢谢!

推荐答案

阵反序列化第一。然后,所有内部反序列化完成。所以,当您尝试访问项目,它们是空。

Array deserializes first. Then all inner deserialization is done. So when you try to access items, they are null.

和主意,用 [OnDeserialized] 属性的一些方法,即建立了所有其他properies。这里是例子:

And idea to use [OnDeserialized] Attribute on some method, that builds up all other properies. And here is example:

[Serializable]
public class TestClass : ISerializable
{
    private Trade[] _innerList;
    public ObservableCollection<Trade> List { get; set; }

    public TestClass()
    { }

    [OnDeserialized]
    private void SetValuesOnDeserialized(StreamingContext context)
    {
        this.List = new ObservableCollection<Trade>(_innerList);
        this._innerList = null;
    }

    protected TestClass(SerializationInfo info, StreamingContext context)
    {
        var value = info.GetValue("inner", typeof(Trade[]));
        this._innerList = (Trade[])value;
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("inner", this.List.ToArray());
    }
}

这篇关于数组的反序列化总是给人空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:08