English 中文(简体)
添加了“同一关键物品”的错误,添加了“protobuf-net”。
原标题:"An item with the same key has already been added" error with protobuf-net

I m试图用C#的代用代用法取代现有序列器,用马克·科斯塔尔取代。 我的法典内容广泛,我的目标是能够在变化最小的情况下完成这一转变。

我在谈到一个问题时认为,它为什么会发生,但需要帮助克服——特别是需要尽可能少地改变我现有的法典和类别的解决办法。 我的守则是复杂的,因此我创建了以下简短的样本,以表明问题:

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

        }
    }

}
}

简言之,当母物体创建时,它就产生了一个带有某些价值的dict子。 我假定,当Protobuf试图重建该物体时,它使用同样的构造者(也用价值发动独裁者),然后试图将同样的价值再次推向帝国——和错误的一部分。

How can I overcome it with minimal changes to my code?

Kind regards, Yossi.

最佳回答

这里最容易的选择可能是:

[ProtoContract(SkipConstructor = true)]

正如它所言,这将不执行施工者(或外地初始者)。 请注意,若无数据,该词将告无效。 另一种做法可能是使用序号回击(这在装上数据之前就起火):

[ProtoBeforeDeserialization]
private void Foo()
{
    Dict.Clear();
}

第三个选择是利用:

[ProtoContract(SkipConstructor = true)]

并且

[ProtoAfterDeserialization]
private void Foo()
{
    if(Dict == null) Dict = new Dictionary<int,string>();
}

即便没有数据,也默认该词为空字典。 您需要从<代码>中删除。 父亲也因为使用了默认<代码> Son Constructionor.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签