C# .net 协议缓冲区 - protobuf-net 支持序列化对象值字典吗? [英] C# .net protocol buffers - protobuf-net support for serializing dictionary of object values?

查看:101
本文介绍了C# .net 协议缓冲区 - protobuf-net 支持序列化对象值字典吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm new to protocol buffers and I am using protobuf-net for VS2010. from what i'm reading here Dictionary in protocol buffers, it doesn't seem that protobuf can serialize a dictionary with object types as values. but here on his site i read this:

类型说明

支持:

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

custom classes that: are marked as data-contract have a parameterless constructor for Silverlight: are public many common primitives etc single dimension arrays: T[] List / IList Dictionary / IDictionary any type which implements IEnumerable and has an Add(T) method The code assumes that types will be mutable around the elected members. Accordingly, custom structs are not supported, since they should be immutable.

似乎支持它.

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

I can successfully compile a List of objects like so:

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

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

This works fine for a List<SomeClass>. Why cannot I serialize using protobuf-net a Dictionary<int, SomeClass>? What would the message look like to serialize a Dictionary<int, SomeClass>?

推荐答案

Dictionary 与 protobuf-net 完美结合.Protobuf-net 在代码优先时工作最简单,所以:*在你的模型中有一个 Dictionary.您不需要使用 .proto - 主要用于跨平台目的..proto 规范没有字典的概念,但如果你需要使用 .proto 模式,那么它被序列化为:

A Dictionary<int,SomeClass> is perfectly serailizable with protobuf-net. Protobuf-net works simplest when working code-first, so: *just have a Dictionary<int,SomeClass> in your model. You are not required to use a .proto at all - that is provided mainly for cross-platform purposes. The .proto specification has no concept of a dictionary, but if you are required to use a .proto schema, then this is serialized as:

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

以字典为

repeated KeyValuePairInt32SomeClass YourDictionary = [n]; 

这篇关于C# .net 协议缓冲区 - protobuf-net 支持序列化对象值字典吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆