我读过很多关于 protobuf-net 继承功能的问题。
我只是想知道是否可以像使用 [ProtoContract],[ProtoMember] 一样使用 [DataContract],[DataMember]。为什么我不能使用 [KnowType] 而不是使用 [ProtoInclude]?
我提出这个问题是因为我已经使用 [DataContract],[DataMember] 进行 protobuf-net 的序列化。无需添加“Protobuf-net”。它仅使用“System.Runtime.Serialization”。
但是...现在如果我的类需要从某个类继承,我是否必须为 [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
最后,我想知道如果我有 100 个子类,我会不会让自己发疯,在基类中添加 100 个 [ProtoInclude] 标签?
提前感谢您的帮助
vee
编辑:这在 v2 中不再需要 - 您可以在运行时指定它,或者使用DynamicType
.
原因是 protobuf 传输格式(由 Google 设计)不包含任何类型元数据,因此我们需要some了解我们正在谈论的对象类型的方法。[KnownType]
不提供此信息,并且没有明确的方法来独立提供可靠的密钥。
其实protobuf不支持继承either- protobuf-net 通过将子类型视为嵌套消息来解决这个问题。所以一个ChildClass1
实际上出现在途中好像BlahBlahBlah
是子对象的属性,有点像:
message BaseClass {
optional ChildClass1 ChildClass1 = 1;
optional SomeOtherSubType SomeOtherSubType = 2;
}
message ChildClass1 {
optional string BlahBlahBlah = 1;
}
etc
重新省略;在“v2”中,您可以选择通过自己的代码在类型模型之外指定此数据。这意味着您不需要装饰所有内容,但仍然需要某种机制将键与类型相关联。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)