【问题标题】:ProtoBuf-Net error message - "Nested or jagged lists and arrays are not supported"ProtoBuf-Net 错误消息 - “不支持嵌套或锯齿状列表和数组”
【发布时间】:2013-09-25 17:00:49
【问题描述】:

主要目标是动态填充字典对象并使用 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();
    }

}

【问题讨论】:

  • 请注意,该对象被序列化了两次,使用不同的 API。

标签: c# protocol-buffers protobuf-net


【解决方案1】:

您可以将 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; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2023-03-04
    • 2014-03-16
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多