使用 Protobuf-Net 序列化未知子类型

Serializing Unknown Sub-Types With Protobuf-Net

我现在正在努力了解 protobuf-net,发现了这篇关于能够序列化子类型的文章:How to Serialize Inherited Class with ProtoBuf-Net

实际上这表明基类型需要了解子类型:

[ProtoContract]
[ProtoInclude(1, typeof(SubType))]
class BaseType { ... }

class SubType : BaseType { ... }

问题:

  1. 子类型未知怎么办?
  2. protobuf-net 能否针对特定类型及其子类型自动配置(不知道它们)?
  3. 第三,是否有类似 Fluent-API 的东西来配置 protobuf-net,而不是使用属性?
  1. 是的,在 RuntimeTypeModel 下有一个完整的 API,包括在运行时而不是提前进行自动发现的回调

但是!

不,它不能处理未知的子类型,除非你的意思是:完全忽略子类型方面并将其视为已知类型。