c# - ProtoBuf-Net 错误消息 - "Nested or jagged lists and arrays are not supported"

标签 c# protocol-buffers protobuf-net

主要目标是动态填充字典对象并使用 Protobuf-net 对其进行序列化,然后通过 WCF 服务将其发送到客户端。

@marc 你能否告诉我一个代码示例,说明当我尝试使用 Protobuf-net 进行序列化时,如何解决错误“不支持嵌套或锯齿状列表和数组”

[Serializable]
[ProtoContract]
public class DynamicWrapper
{
    [ProtoMember(1)]
    public List<Dictionary<string, string>> Items { get; set; }

    public DynamicWrapper()
    {
        Items = new  List<Dictionary<string, string>>();
    }
}

public class Service1 : IService1
{
    public byte[] GetData(string sVisibleColumnList)
    {
        DynamicWrapper dw = new DynamicWrapper();
        Dictionary<string, string> d = new Dictionary<string, string>();
        d.Add("CUSIP", "123456");
        dw.Items.Add(d);

        d = new Dictionary<string, string>();
        d.Add("ISIN", "456789");
        dw.Items.Add(d);
        var ms = new MemoryStream();
        var model = ProtoBuf.Meta.RuntimeTypeModel.Default;
        model.Serialize(ms, dw);
        Serializer.Serialize <DynamicWrapper>(ms, dw);
        return ms.GetBuffer();
    }

}

最佳答案

您可以将 Dictionary 包装到一个单独的类中。然后使用这些对象的列表。

[ProtoContract]
public class DynamicWrapper
{
    [ProtoMember(1)]
    public List<DictWrapper> Items { get; set; }

    public DynamicWrapper()
    {
        Items = new  List<DictWrapper>();
    }
}

[ProtoContract]
public class DictWrapper
{
    [ProtoMember(1)]
    public Dictionary<string, string> Dictionary { get; set; }
}

关于c# - ProtoBuf-Net 错误消息 - "Nested or jagged lists and arrays are not supported",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19010992/

相关文章:

c# - 在 C# 中解析原始 Protocol Buffer 字节流

c# - Asp.net: session 如何在服务器场环境中工作

c# - 向实体 LINQ 查询添加条件(字符串)

c# - 在 C# 中,在性能方面复制位的最佳方法

serialization - protobuf 消息的分隔符是什么?

ubuntu - 未找到时间戳 proto 和其他 proto 文件,即使它们存在于/usr/local/bin/include/google/protobuf

c# - 来自现场设备的 UDP 数据包不会通过 azure 基础设施到达我的服务

ios - 构建 XCTest 时找不到文件错误

c# - protobuf 和 List<object> - 如何序列化/反序列化?

c# - protobuf-net 是否支持 ArraySegment(还?)