PortoBuf-net反序列化序列化双数组的双精度版本 [英] PortoBuf-net Deserializes a double-sized version of the serialized double array

查看:52
本文介绍了PortoBuf-net反序列化序列化双数组的双精度版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Serializable]
[ProtoContract]
public class DataWrapper
{
    [ProtoMember(1)]
    public double[] Data = new double[] { 1, 2, 3, 4 };
}

class Program
{
    static void Main(string[] args)
    {

        Dictionary<int, DataWrapper> serialized = new Dictionary<int, DataWrapper>();
        Dictionary<int, DataWrapper> deserialized;// = new Dictionary<int, OHLC>();
        for (int i = 0; i < 10; i++)
        {
            serialized.Add(i, new DataWrapper());                
        }
        using (FileStream ms = new FileStream("dictionary", FileMode.Create, FileAccess.Write))
        {
            Serializer.Serialize<Dictionary<int, DataWrapper>>(ms, serialized);
        }


        using (FileStream ms = new FileStream("dictionary", FileMode.Open, FileAccess.Read))
        {
            deserialized = Serializer.Deserialize<Dictionary<int, DataWrapper>>(ms);
        }

        Console.WriteLine("serialized {0} and deserialized {1}", serialized[0].Data.Length, deserialized[0].Data.Length);

        }
}

我希望对数组进行反序列化长度为4,而我收到的是长度为8的数组。这是一个错误,还是我在这里做错了?

I am expecting to Deserialize an array of length 4, while I received an array of length 8. Is this a bug, or am I doing a mistake here?

请注意,该代码是没有意义的。这只是为了解释我在实际情况中面临的问题

Note that the code is meaningless. It's just an attempt to explain the issue I am facing in my real scenario

推荐答案

我不是Protobuf-net专家,但这似乎与此问题有关。我怀疑这是由于您明确初始化 double [] 来包含4个元素,而protobuf-net在反序列化过程中追加了另外4个元素。

I am no Protobuf-net expert, but this seems to be related to this issue. I suspect it is due to the fact that you're explicitly initializing the double[] to have 4 elements, and protobuf-net "appends" the additional 4 elements during deserialization.

作为解决方法,您可以在 ProtoContract <中设置 SkipConstructor = true 选项。 / code>,然后按预期运行:

As a workaround, you can set SkipConstructor=true the option in the ProtoContract and then it works as you expect:

[ProtoContract(SkipConstructor=true)]

这篇关于PortoBuf-net反序列化序列化双数组的双精度版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆