小编yos*_*sic的帖子

"具有相同键的项目已添加"与protobuf-net的错误

我正在尝试用Marc Gravell的C#替换现有的序列化程序和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); …
Run Code Online (Sandbox Code Playgroud)

c# dictionary protocol-buffers protobuf-net

6
推荐指数
1
解决办法
2503
查看次数

使用protobuf为c#反序列化"long"字符串对我来说无法正常工作

我显然做了一些基本的错误,但我无法弄明白,也找不到文档.

我正在尝试使用Marc Gravell的.NET for proto-buf,并尝试序列化和反序列化对象.一旦一个对象包含一个"太长"的字符串(没有试图确定大小阈值,但它只有几百个字节),字符串不会为我正确反序列化.

这是我的代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using ProtoBuf;

namespace ConsoleApplication1
{
public class Program
{
    [ProtoContract]
    public class test
    {
        [ProtoMember(1)]            
        public int i;
        [ProtoMember(2)]
        public string s1;
        [ProtoMember(3)]
        public string s2;
        [ProtoMember(4)]
        public char[] arrchars;
        [ProtoMember(5)]
        public Dictionary<int, string> Dict = new Dictionary<int, string>();

    }


    static void Main(string[] args)
    {
        test var1 = new test();
        var1.i = 10;
        var1.s1 = "Hello";
        var1.arrchars = new char[] …
Run Code Online (Sandbox Code Playgroud)

c# protobuf-net

4
推荐指数
1
解决办法
1796
查看次数

标签 统计

c# ×2

protobuf-net ×2

dictionary ×1

protocol-buffers ×1