为什么我必须使用 [ProtoInclude]? [英] Why I have to use [ProtoInclude]?

查看:44
本文介绍了为什么我必须使用 [ProtoInclude]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于 protobuf-net 中继承特性的问题.我只是想知道是否可以像使用 [ProtoContract],[ProtoMember] 一样使用 [DataContract],[DataMember].为什么我不能使用 [KnowType] 而不是使用 [ProtoInclude]?

I have read many questions about inheritance feature in protobuf-net. I just wonder that if I can use [DataContract],[DataMember] in the same way of using [ProtoContract],[ProtoMember]. Why I could not use [KnowType] instead of using [ProtoInclude]?

我提出这个问题是因为我已经将 [DataContract],[DataMember] 用于 protobuf-net 的序列化.无需添加Protobuf-net".它只使用了System.Runtime.Serialization".

I raise this question because I used [DataContract],[DataMember] for protobuf-net's serialization already. There was no need to add "Protobuf-net". It used only "System.Runtime.Serialization".

但是...现在如果我的类需要从某个类继承,我是否必须为 [ProtoInclude] 属性添加Protobuf-net"?例如,

But... Now if my class need to inherit from some class, do I have to add "Protobuf-net" for [ProtoInclude] attribute? for example,

using System.Runtime.Serialization;
namespace test
{

[DataContract]
/// [KnowType(typeof(SomeClass))]
/// or
/// [ProtoInclude(100,typeof(SomeClass))]
public class BaseClass
{
   //...
   [DataMember(Order=1)]
   public string BlahBlahBlah {get; set;}
}

[DataContract]
public class ChildClass1 : BaseClass
{
   //...
   [DataMember(Order=1)]
   public string BlahBlahBlah {get; set;}
}
}// end namespace

最后,我想知道如果我有 100 个子类,在基类中添加 100 个 [ProtoInclude] 标签会不会让我发疯?

感谢广告中的任何帮助

vee

推荐答案

这在 v2 中不再需要 - 你可以在运行时指定它,或者使用 DynamicType.

this is no longer required in v2 - you can specify this at runtime, or use DynamicType.

这样做的原因是 protobuf 有线格式(由 Google 设计)不包含任何类型元数据,因此我们需要某种方法来了解我们正在谈论的对象类型.[KnownType] 不提供此信息,也没有明确的方法可以独立提供健壮的密钥.

The reason for this is that the protobuf wire format (devised by Google) does not include any type metadata, and so we need some way of knowing what type of object we are talking about. [KnownType] doesn't provide this information, and there is no clear way of providing a robust key independently.

实际上,protobuf 不支持继承要么 - protobuf-net 通过将子类型视为嵌套消息来解决这个问题.所以一个 ChildClass1 实际上出现在 intransit 就好像 BlahBlahBlah 是一个子对象的属性,有点像:

Actually, protobuf doesn't support inheritance either - protobuf-net shims around that by treating sub-types as nested messages. So a ChildClass1 actually appears in transit as though BlahBlahBlah was a property of a sub-object, a bit like:

message BaseClass {
    optional ChildClass1 ChildClass1 = 1;
    optional SomeOtherSubType SomeOtherSubType = 2;
}
message ChildClass1 {
    optional string BlahBlahBlah = 1;
}

重新省略;在v2"中,您可以选择通过自己的代码在类型模型之外指定此数据.这意味着您不需要装饰所有东西,但它仍然需要某种机制来将键与类型相关联.

Re omitting it; in "v2", you have the option of specifying this data outside of the type model, via your own code. This means you don't need to decorate everything, but it still needs some mechanism to associate keys with types.

这篇关于为什么我必须使用 [ProtoInclude]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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