protobuf-net继承

Ego*_*hin 12 c# protocol-buffers protobuf-net

Marc在stackoverflow上提到,在protobuf-net的v2中,可以使用ProtoInclude属性(或类似的方法)来序列化/反序列化类层次结构,而无需在基类中指定每个子类型.这是实现了吗?我们有一个可以在外部库中派生的插件接口,因此无法知道派生类型是什么.我们可以在类型之间保持唯一的编号,但我在网上找不到任何示例,缺少使用ProtoInclude属性,需要指定子类型.

如果我不知道子类型是什么,我将如何使用protobuf-net实现继承?

Mar*_*ell 19

如果你不能在属性中指定子类型(因为它在编译时是不知道的)你有2个选项(两个选项都只适用于"v2",可用作beta):

  1. 使用a RuntimeTypeModel而不是静态Serializer方法(现在只是一个简短的方法RuntimeTypeModel.Default); 告诉模型继承(下面的例子)
  2. 添加DynamicType = true[ProtoMember(...)]问题中

第二个不是非常纯粹的protobuf - 它嵌入类型信息,我不喜欢,但人们只是一直要求.第一个是我的首选.要在运行时添加子类型:

var model = TypeModel.Create();
var type = model.Add(typeof(YourBaseType), true);
var subTypeA = model.Add(typeof(SomeSubType), true);
var subTypeB = model.Add(typeof(SomeOtherSubType), true);
type.AddSubType(4, typeof(SomeSubType));
type.AddSubType(5, typeof(SomeOtherSubType));
Run Code Online (Sandbox Code Playgroud)

true上述装置"使用正常规则来自动添加成员属性" -也可以利用这一控制和指定的属性(等),如果手动你喜欢.

请注意,TypeModel应该缓存并重新使用(不需要为每个需要序列化的对象创建),因为它包含一些"emit"代码来生成方法.重新使用它会更快,并且需要更少的内存.类型模型是线程安全的,可用于在不同线程上并发地序列化/反序列化多个流.


3Pi*_*3Pi 5

为了进一步扩展Marc的答案,特别是处理RuntimeTypeModel,这是编写它的一种方法:

RuntimeTypeModel.Default[typeof(BaseClass)].AddSubType(20, typeof(DerivedClass));
Run Code Online (Sandbox Code Playgroud)

如果您有更多派生自派生类的类,请将它们链接起来

RuntimeTypeModel.Default[typeof(DerivedClass)].AddSubType(20, typeof(DerivedFromDerivedClass ));
Run Code Online (Sandbox Code Playgroud)

等等.
然后Serializer.Serialize(file,object),您可以像使用protobuf-net一样使用它.
这适用于项目和名称空间.