protobuf-net v2 类型元

发布于 2024-11-23 23:58:08 字数 839 浏览 0 评论 0原文

根据 这篇 帖子(从 3 月开始) ),protobuf v2 允许我们解析流中的类型。由于v2现在已经是beta 5,我认为这个功能已经实现了,所以我想知道如何使用这个新功能。我无法找到任何有关它的文档,因此我们将不胜感激!

输入元数据

<块引用> <块引用>

序列化很好,但我不知道(也不可能知道)所有 我前面的类型。我怎样才能做到这一点?

嗯,protobuf 是一种基于合约的格式;如果你不知道 类型,它会很挣扎——就像任何基于合约的序列化器一样......

<块引用> <块引用>

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

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

它远远超出了核心 protobuf 规范,它会闪烁警告 BinaryFormatter 的迹象,我的克星但是,很多人似乎都想要 我想我必须克服这一点;但按照我的条件!所以在 v2 中,我是 添加指示(在每个成员的基础上)对象的能力 应该从流中解析它们的类型信息。默认情况下,由 嵌入程序集限定名称,但提供抽象 在此层之上允许您提供自己的字符串<====>类型映射 (这样就避免了因类型太多而造成胃部打结 依赖)。

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 !

Type meta

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

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:

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很糊涂小朋友 2024-11-30 23:58:08

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

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

Re the "string<===>Type map”,即 TypeModel 上的 DynamicTypeFormatting 事件。如果您使用 Serializer.* 方法,这是 RuntimeTypeModel.Default 序列化器实例的快捷方式(主要用于保留 v1 API)。

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

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

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

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

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;}

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)

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 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文