protobuf-net v2类型元 [英] protobuf-net v2 type meta

查看:100
本文介绍了protobuf-net v2类型元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据帖子(从3月开始),protobuf v2允许我们解析流中的类型.由于v2现在处于beta 5中,我认为该功能已经实现,因此我想知道如何使用此新功能.我还没有找到任何文档,因此我们将不胜感激!

According to this post (from March), protobuf v2 allows us to resolve types from a stream. Since v2 is now in beta 5, I think this feature has already been implemented, so I was wondering how to use this new feature. I haven't been able to find any documentation on it, so some help would be greatly appreciated !

类型元

序列化很好,但是我不知道(也不知道)所有 我的类型预先.我该怎么办?

The serialization is fine, but I don’t know (and cannot know) all of my types up front. How can I do this?

那么protobuf是一种基于合同的格式;如果你不知道 类型,这会很困难–任何基于合约的序列化器都会如此……

Well, protobuf is a contract based format; if you don’t know the types, it will struggle – as will any contract based serializer…

是的,我明白了;现在:我该怎么办?

Yes, I get that; now: how do I do it?

现在,由于各种原因,我推迟了在流中添加任何元数据:

Now, I’ve held off putting any meta in the stream for various reasons:

它远远超出了Protobuf核心规范,它发出了警告 BinaryFormatter的迹象,我的宿敌但是,似乎有很多人想要 我认为我必须扣紧;但就我而言!所以在第二版中 添加指示(按成员)对象的功能 应该从流中解析其类型信息.默认情况下 嵌入程序集限定名称,但提供抽象 允许您提供自己的字符串的图层< ===>类型映射 (从而避免因过多的类型而引起的胃部打结 依赖性).

it steps far outside the core protobuf spec it flashes the warning signs of BinaryFormatter, my nemesis But, so many people seem to want this that I think I have to buckle; but on my terms! So in v2, I’m adding the ability to indicate that (on a per-member basis) objects should resolve their type information from the stream. By default, by embedding the assembly-qualified-name, but providing an abstraction layer over that allowing you to provide your own string<===>Type map (and thus avoiding the knots in by stomach caused by too much type dependency).

推荐答案

这里的窍门是在成员上使用DynamicType = true选项来您的对象-作为一个过于简化的示例:

The trick here is to use the DynamicType = true option on a member to your object - as an over-simplified example:

[ProtoMember(12, DynamicType = true)]
public object CouldBeAnything {get;set;}

重新获得字符串< ===>类型映射",即TypeModel上的DynamicTypeFormatting事件.如果使用的是Serializer.*方法,则这是RuntimeTypeModel.Default序列化程序实例的快捷方式(主要用于保留v1 API).

Re the "string<===>Type map", that is the DynamicTypeFormatting event on TypeModel. If you are using the Serializer.* methods, that is a shortcut (mainly for preserving the v1 API) to the RuntimeTypeModel.Default serializer instance.

(作为一个附带说明,在写这篇文章时,我确实注意到了一个边缘情况,需要在代码中进行修复)

(as a side-note, in writing this I did notice an edge-case that I need to go and fix in the code)

注意:这里的另一种方法,而不是使用DynamicType,是简单地在运行时配置模型,例如:

Note: another approach here, rather than using DynamicType, is to simply configure the model at runtime, for example:

var knownTypes = GetMyKnownTypesAtRuntimeWithUniqueIdentifiers();
var metaType = typeModel[typeof(MyBaseClass)];
foreach(var knownType in knownTypes)
{
    metaType.AddSubType(knownType.UniqueIdentifier, knownType.Type);
}

IMO,后者是我的首选选项,通常效率更高.请注意,唯一标识符必须是固定的/可重复的,因为这是有线格式的一部分(不要只使用找到它们的顺序的索引).

IMO this latter is my preferred option, and will generally be more efficient. Note that it is necessary for the unique-identifiers to be fixed/repeatable, as that is part of the wire format (don't just use the index of the order you find them).

这篇关于protobuf-net v2类型元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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