Search code examples
c#protocol-buffersprotobuf-net

Serializing Unknown Sub-Types With Protobuf-Net


I'm trying to get my head around protobuf-net at the moment, and found this article on being able to serialize sub-types: How to Serialize Inherited Class with ProtoBuf-Net

Effectively this suggests that the base type needs to know about the sub-type:

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

class SubType : BaseType { ... }

Questions:

  1. What if the sub-type is unknown?
  2. Can protobuf-net be automatically configured for a particular type and its sub-types (without knowing them)?
  3. Thirdly, is there something like a Fluent-API for configuring protobuf-net, instead of using attributes?

Solution

    1. Yes, there is a full API for this under RuntimeTypeModel, including callbacks for auto-discovery during runtime rather than ahead of time

    However!

    No, it can't work with unknown subtypes unless you mean: by ignoring the subtype aspect completely and just treating it as though it were the known type.