什么是ProtoInclude属性的意思是(在protobuf网) [英] What does the ProtoInclude attribute mean (in protobuf-net)

查看:234
本文介绍了什么是ProtoInclude属性的意思是(在protobuf网)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protobuf网的实施,什么是 ProtoInclude 属性的意思,和它有什么作用?

In the ProtoBuf-Net implementation, what does the ProtoInclude attribute mean, and what does it do?

一个例子是AP preciated

An example would be appreciated.

我看到它在这个岗位,我不知道是什么它的作用。这个例子是:

I saw it in this post and I'm not sure what it does. The example was:

[Serializable,
 ProtoContract,
 ProtoInclude(50, typeof(BeginRequest))]
abstract internal class BaseMessage
{
  [ProtoMember(1)]
  abstract public UInt16 messageType { get; }
}

[Serializable,
 ProtoContract]
internal class BeginRequest : BaseMessage
{
    [ProtoMember(1)]
    public override UInt16 messageType
    {
        get { return 1; }
    }
}

此外,有没有一种方法使用的硫辛酸工具来生成这样的继承?

Also, is there a way to generate such inheritance using the protogen tool?

推荐答案

对不起,我不是故意错过这一个 - 唉,我没有看到的一切

Sorry, I didn't mean to miss this one - alas, I don't see everything.

鉴于该问题的具体情况,我会假设你至少passingly熟悉.proto;纠正我,如果我错了。

Given the specifics in the question, I'm going to assume that you are at least passingly familiar with .proto; correct me if I am wrong.

[ProtoInclude] 作品很多像 [XmlInclude] 的XmlSerializer - 或 [KnownType] 的DataContractSerializer - 这允许它在识别一类的子类(DE )序列化。唯一的额外的事情是,它需要一个标签(编号)来识别每个子类型(也必须是唯一的,并且不发生冲突的任何从父类型的字段)。

[ProtoInclude] works a lot like [XmlInclude] for XmlSerializer - or [KnownType] for DataContractSerializer - it allows it to recognise subclasses of a type during (de)serialization. The only additional thing is that it needs a tag (number) to identify each sub-type (that must be unique, and not clash with any of the fields from the parent type).

回复硫辛酸:没了;基础规范(由谷歌),使得继承没有规定的所有的,所以硫辛酸(通过.proto)有没有机制来EX preSS这一点。 protobuf网提供继承支持作为的扩展的,但确实它的方式,仍有消息线兼容其他实现。在一推,的也许的,我可以通过在谷歌规范新扩展属性添加硫辛酸的支持,但是我没有这样做呢。

Re protogen: nope; the underlying spec (by google) makes no provision for inheritance at all, so protogen (via .proto) has no mechanism to express this. protobuf-net provides inheritance support as an extension, but does it in a way that still leaves the messages wire-compatible with the other implementations. At a push, maybe I could add protogen support via the new extension properties in the google spec, but I haven't done this yet.

所以,看例子;即前presses之间的继承关系 BaseMessage 的BeginRequest ;不管你是否做的:

So; to look at the example; that expresses an inheritance relationship between BaseMessage and BeginRequest; regardless of whether you do:

Serialize<BaseMessage>(...)
Serialize<BeginRequest>(...)

  • 无论哪种方式,它将开始在基地( BaseMessage )而努力向上;这是不是的完全的真 - 它写的数据的开始的BeginRequest (因此它知道我们有一个的BeginRequest 尽早反序列化过程)。重要的是,从任何父合同类型的字段包括在内,串行着眼于的实际的对象是传入的 - 不只是你的说的类型的是
    • either way, it will start at the base (BaseMessage) and work upwards; which isn't exactly true - it writes the data starting with BeginRequest (so that it knows we have a BeginRequest as early as possible during deserialization). The important thing is that the fields from any parent contract types is included, and the serializer looks at the actual object passed in - not just the type you say it is.
    • 同样,deserilaization过程中,无论是否使用:

      Likewise, during deserilaization, regardless of whether you use:

      Deserialize<BaseMessage>(...)
      Deserialize<BeginRequest>(...)
      

      你会得到你真正序列化的类型(presumably一个的BeginRequest )。

      引擎盖下的,为了兼容(与广Protocol Buffers的规范),这是类似于编写类似(原谅任何错误,我.proto是生锈):

      Under the bonnet, for compatibility purposes (with the wide protocol buffers specification), this is similar to writing something like (forgive any errors, my .proto is rusty):

      message BaseMessage {
          optional BeginRequest beginRequest = 50;
          optional uint32 messageType = 1;   
      }
      message BeginRequest {        
      }
      

      (倍率或许不应该指定 [ProtoMember] ,顺便说一句。

      通常情况下,它会写升序标签字段顺序,反而使高效的反序列化引擎厚脸皮选择写的子类数据的第一的(这是明确的规范允许的) - 即它写像(你必须想象二进制...):

      Normally, it would write fields in ascending tag order, but to make for efficient deserialization the engine cheekily chooses to write the subclass data first (which is explicitly allowed by the spec) - i.e. it writes something like (you'll have to imagine the binary...):

      [tag 50, string][length of sub-message][body of sub-message][tag 1, int][value]
      

      (在此情况下,子消息的主体为空)

      (in this case, the body of the sub-message is empty)

      这是否覆盖它?

      这篇关于什么是ProtoInclude属性的意思是(在protobuf网)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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