[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的数组。这是一个错误,还是我在这里犯了一个错误?
请注意,代码没有意义。这只是试图解释我在真实场景中面临的问题
答案 0 :(得分:1)
我不是Protobuf-net专家,但这似乎与this issue有关。我怀疑这是因为您明确初始化double[]
以包含4个元素,而protobuf-net在反序列化期间“附加”了额外的4个元素。
作为解决方法,您可以在SkipConstructor=true
中设置ProtoContract
选项,然后按预期运行:
[ProtoContract(SkipConstructor=true)]