C#.net协议缓冲区 - protobuf-net支持序列化对象值字典?

ijj*_*jjo 8 c# protocol-buffers protobuf-net

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

关于类型的说明

支持的:

自定义类:标记为数据契约具有Silverlight的无参数构造函数:公共许多常见原语等单维数组:T [] List/IList Dictionary/IDictionary任何实现IEnumerable并具有Add(T)方法的类型代码假定类型在选定成员周围是可变的.因此,不支持自定义结构,因为它们应该是不可变的.

这似乎得到了支持.

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

message ValuesObject {
    optional int32 SomeVal = 1;
    repeated SomeClass ListOfSomeClassTypes = 2;
}
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*ell 9

A Dictionary<int,SomeClass>与protobuf-net完全相容.Protobuf-net 在代码优先工作时最简单,因此:*只需Dictionary<int,SomeClass>在模型中使用.您不需要使用.proto 在所有 -这主要是为了跨平台的目的..proto规范没有字典的概念,但如果您需要使用.proto模式,则将其序列化为:

message KeyValuePairInt32SomeClass {
    required int32 Key = 1;
    required SomeClass Value = 2;
}
Run Code Online (Sandbox Code Playgroud)

用字典作为

repeated KeyValuePairInt32SomeClass YourDictionary = [n]; 
Run Code Online (Sandbox Code Playgroud)