1

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?
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

1 Answers1

1
  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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks Marc! Could you point me in the right direction for some documentation on this, please? – Matthew Layton Sep 03 '21 at 19:09
  • @Matthew I'm not sure I've ever written longform documentation on that aspect. The API and examples are there. Is there something specific you're trying to do? – Marc Gravell Sep 04 '21 at 09:43
  • Basically, I'm building something that auto-configures all sub-types for serialization based on a convention that it will serialize all public instance properties of those sub-types. I've also tweeted you something in relation to this and record types. – Matthew Layton Sep 04 '21 at 11:53
  • I've forked the protobuf-net repo so I'll do some digging :-) – Matthew Layton Sep 04 '21 at 11:53