我想用Marc Gravell的C#protobuf替换现有的序列化程序。我的代码非常广泛,我的目标是能够以最少的更改完成切换。“一个项目具有相同的密钥已被添加”与protobuf网错误
我遇到了一个问题,我相信我明白了为什么会发生这种情况,但需要帮助克服 - 尤其是需要对现有代码和类进行最少更改的解决方案。我的代码是复杂的,所以我创建了以下简短的示例来演示该问题:
using System;
using System.Collections.Generic;
using System.IO;
using ProtoBuf;
namespace ConsoleApplication1
{
class program_issue
{
[ProtoContract]
public class Father
{
public Father()
{
sonny = new Son();
}
[ProtoMember(101)]
public string Name;
[ProtoMember(102)]
public Son sonny;
}
[ProtoContract]
public class Son
{
public Son()
{
Dict.Add(10, "ten");
}
[ProtoMember(103)]
public Dictionary<int, string> Dict = new Dictionary<int, string>();
}
static void Main(string[] args)
{
Father f1 = new Father();
f1.Name = "Hello";
byte[] bts = PBSerializer.Serialize(typeof(Father), f1);
Father f2;
PBSerializer.Deserialize(bts, out f2);
}
public static class PBSerializer
{
public static byte[] Serialize(Type objType, object obj)
{
MemoryStream stream = new MemoryStream();
ProtoBuf.Serializer.Serialize(stream, obj);
string s = Convert.ToBase64String(stream.ToArray());
byte[] bytes = stream.ToArray();
return bytes;
}
public static void Deserialize(byte[] data, out Father obj)
{
using (MemoryStream stream = new MemoryStream(data))
{
obj = ProtoBuf.Serializer.Deserialize<Father>(stream);
}
}
}
}
}
总之,在创建父对象时,它创建inits一些值的字典子对象。我认为,当protobuf在反序列化时试图重建对象时,它使用相同的构造函数(因此也启动带有值的字典),然后尝试再次推送相同的值作为反序列化 - >错误的一部分。
如何通过对代码进行最少更改来克服它?
亲切的问候, Yossi。
小调:“protobuf-net”只是一个实现;还有其他C#protobuf实现。我只是提到这个来解释为什么我改变了标题 –