child 类 不在同一个程序集中时如何在 protobuf 中列出继承
How to list inheritance in protobuf when child classes are not in same assembly
在我们的应用程序中,我们有不同包中的模型(将使用 protobuf 通过 WCF 传输),几乎所有 childs 项目都引用了 "common" 项目。在 childs 项目中,我们发现所有 classes 特定于某些业务领域。
所有的DLL都不发货,要看客户购买的是什么功能。
我知道指定可能性 childs classes 的一种方法是:
[ProtoContract]
[ProtoInclude(100, typeof(Derived))]
[ProtoInclude(101, typeof(Derive2))]
public class Base {
[ProtoMember(Order=1)]
int Old;
}
但在我的情况下,我无法引用包含 Derived
和 Derived2
classes 的项目(这会导致循环引用,更重要的是,这很奇怪parent 必须知道其所有 child).
使用默认 DataContractSerializer
可以指定一种方法来查找我们正在使用的 class 类型,有没有办法实现这一点?或者我不能在我的限制条件下使用 protobuf?
it's weird that the parent has to know all its child.
不,这其实很正常;另请参阅 [XmlInclude]
和 [KnownType]
,它们的工作方式相同
但是,要避免 typeof
的循环引用问题,您可以使用 程序集限定名 作为字符串。获取程序集限定名,看typeof(Foo).AssemblyQualifiedName
写的是什么,然后:
[ProtoInclude(100, "your assembly qualified name here")]
[ProtoInclude(101, "another assembly qualified name here")]
另一种选择是每次在运行时使用 RuntimeTypeModel.Default[typeof(RootType)].AddSubType(...)
配置 - 但是:您仍然需要能够使用可靠的整数可靠地配置它。
在我们的应用程序中,我们有不同包中的模型(将使用 protobuf 通过 WCF 传输),几乎所有 childs 项目都引用了 "common" 项目。在 childs 项目中,我们发现所有 classes 特定于某些业务领域。
所有的DLL都不发货,要看客户购买的是什么功能。
我知道指定可能性 childs classes 的一种方法是:
[ProtoContract]
[ProtoInclude(100, typeof(Derived))]
[ProtoInclude(101, typeof(Derive2))]
public class Base {
[ProtoMember(Order=1)]
int Old;
}
但在我的情况下,我无法引用包含 Derived
和 Derived2
classes 的项目(这会导致循环引用,更重要的是,这很奇怪parent 必须知道其所有 child).
使用默认 DataContractSerializer
可以指定一种方法来查找我们正在使用的 class 类型,有没有办法实现这一点?或者我不能在我的限制条件下使用 protobuf?
it's weird that the parent has to know all its child.
不,这其实很正常;另请参阅 [XmlInclude]
和 [KnownType]
,它们的工作方式相同
但是,要避免 typeof
的循环引用问题,您可以使用 程序集限定名 作为字符串。获取程序集限定名,看typeof(Foo).AssemblyQualifiedName
写的是什么,然后:
[ProtoInclude(100, "your assembly qualified name here")]
[ProtoInclude(101, "another assembly qualified name here")]
另一种选择是每次在运行时使用 RuntimeTypeModel.Default[typeof(RootType)].AddSubType(...)
配置 - 但是:您仍然需要能够使用可靠的整数可靠地配置它。