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],以同样的方式使用[原始合同],[ProtoMember]。为什么我不能使用[KnowType]而不是使用[ProtoInclude]?
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".
我之所以提出这个问题,是因为我已经使用[DataContract],[DataMember],以获得原buf-net的序列化。没有必要添加“原始网络”。它只使用“System.Runtime.Serialization”。
But... Now if my class need to inherit from some class, do I have to add "Protobuf-net" for [ProtoInclude] attribute? for example,
但是…现在,如果我的类需要从某个类继承,我是否需要为[ProtoInclude]属性添加“Protobuf-net”?例如,
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
finally, I wonder if I have 100 child class, Won't I drive myself crazy adding 100 [ProtoInclude] tags inside base class?
最后,我想知道我是否有100个子类,我是否会让自己疯狂地在基类中添加100个[ProtoInclude]标签?
Thx in adv for any help
在任何帮助下都可以。
vee
三角
1 个解决方案
#1
4
EDIT: this is no longer required in v2 - you can specify this at runtime, or use DynamicType
.
编辑:这不再需要在v2中——您可以在运行时指定它,或者使用DynamicType。
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.
其原因是,原buf线格式(由谷歌设计)不包含任何类型的元数据,因此我们需要某种方式来了解我们正在讨论的对象类型。[KnownType]并没有提供这些信息,也没有明确的方法可以独立地提供一个健壮的密钥。
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:
实际上,protobuf不支持继承性,它通过将子类型作为嵌套的消息来处理。所以一个孩子在运输过程中出现了,就好像BlahBlahBlah是一个子对象的属性,有点像:
message BaseClass {
optional ChildClass1 ChildClass1 = 1;
optional SomeOtherSubType SomeOtherSubType = 2;
}
message ChildClass1 {
optional string BlahBlahBlah = 1;
}
etc
等
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.
再保险省略;在“v2”中,您可以通过自己的代码在类型模型之外指定该数据。这意味着您不需要装饰所有东西,但是它仍然需要一些机制来将键与类型相关联。
#1
4
EDIT: this is no longer required in v2 - you can specify this at runtime, or use DynamicType
.
编辑:这不再需要在v2中——您可以在运行时指定它,或者使用DynamicType。
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.
其原因是,原buf线格式(由谷歌设计)不包含任何类型的元数据,因此我们需要某种方式来了解我们正在讨论的对象类型。[KnownType]并没有提供这些信息,也没有明确的方法可以独立地提供一个健壮的密钥。
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:
实际上,protobuf不支持继承性,它通过将子类型作为嵌套的消息来处理。所以一个孩子在运输过程中出现了,就好像BlahBlahBlah是一个子对象的属性,有点像:
message BaseClass {
optional ChildClass1 ChildClass1 = 1;
optional SomeOtherSubType SomeOtherSubType = 2;
}
message ChildClass1 {
optional string BlahBlahBlah = 1;
}
etc
等
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.
再保险省略;在“v2”中,您可以通过自己的代码在类型模型之外指定该数据。这意味着您不需要装饰所有东西,但是它仍然需要一些机制来将键与类型相关联。