【问题标题】:C# .net protocol buffers - protobuf-net support for serializing dictionary of object values?C# .net 协议缓冲区 - protobuf-net 支持序列化对象值字典吗?
【发布时间】:2012-06-09 02:15:10
【问题描述】:

我是协议缓冲区的新手,我正在为 VS2010 使用 protobuf-net。从我在这里读到的Dictionary in protocol buffers 来看,protobuf 似乎不能将具有对象类型作为值的字典序列化。但在他的网站上我读到了这个:

类型说明

支持:

被标记为数据契约的自定义类具有无参数 Silverlight 的构造函数:是公共的许多常见原语等 一维数组:T[] List / IList Dictionary / IDictionary 任何类型的 实现 IEnumerable 并具有 Add(T) 方法代码假定 that types will be mutable around the elected members.因此, 不支持自定义结构,因为它们应该是不可变的。

好像是支持的。

我可以像这样成功编译对象列表:

message ValuesObject {
    optional int32 SomeVal = 1;
    repeated SomeClass ListOfSomeClassTypes = 2;
}

这适用于List<SomeClass>。为什么我不能使用 protobuf-net a Dictionary<int, SomeClass> 序列化?序列化Dictionary<int, SomeClass> 的消息是什么样的?

【问题讨论】:

    标签: c# protocol-buffers protobuf-net


    【解决方案1】:

    Dictionary<int,SomeClass> 完全可以使用 protobuf-net 进行 serailizable。 Protobuf-net 在代码优先工作时最简单工作,所以:*在你的模型中只有一个Dictionary<int,SomeClass>。您完全不需要使用 .proto - 主要用于跨平台目的。 .proto 规范没有字典的概念,但如果您被要求使用 .proto 模式,那么它被序列化为:

    message KeyValuePairInt32SomeClass {
        required int32 Key = 1;
        required SomeClass Value = 2;
    }
    

    用字典作为

    repeated KeyValuePairInt32SomeClass YourDictionary = [n]; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多