我是协议缓冲区的新手,我正在使用 VS2010 的 protobuf-net 。从我在这里读到的内容协议缓冲区中的字典 https://stackoverflow.com/questions/4194845/dictionary-in-protocol-buffers,protobuf 似乎无法序列化以对象类型作为值的字典。但在他的网站上我读到了这个:
类型注释
支持的:
自定义类: 标记为数据契约 具有无参数
Silverlight 的构造函数:许多常见的原语等都是公共的
单维数组:T[] List / IList
Dictionary / IDictionary 任何类型
实现 IEnumerable 并具有 Add(T) 方法 代码假设
该类型将在当选成员周围发生变化。因此,
不支持自定义结构,因为它们应该是不可变的。
这似乎是受支持的。
我可以成功编译对象列表,如下所示:
message ValuesObject {
optional int32 SomeVal = 1;
repeated SomeClass ListOfSomeClassTypes = 2;
}
这对于一个List<SomeClass>
。为什么我不能使用 protobuf-net 进行序列化Dictionary<int, SomeClass>
?序列化消息会是什么样子Dictionary<int, SomeClass>
?
A Dictionary<int,SomeClass>
可以通过 protobuf-net 完美序列化。 Protobuf 网络工作原理simplest当代码优先工作时,所以:*只要有一个Dictionary<int,SomeClass>
在你的模型中。您不需要使用 .protoat all- 主要是为了跨平台目的而提供的。 .proto 规范没有字典的概念,但是如果你是必要的要使用 .proto 模式,则序列化为:
message KeyValuePairInt32SomeClass {
required int32 Key = 1;
required SomeClass Value = 2;
}
与字典作为
repeated KeyValuePairInt32SomeClass YourDictionary = [n];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)