Marc mentioned on stackoverflow that it will be possible in v2 of protobuf-net to use ProtoInclude attribute (or similar approach) to serialize/deserialize class hierarchy without a need to specify each subtype in the base class. Is this implemented yet? We have a plugin interface that can be derived in external libraries, so there is no way of knowing what the derived types will be. We could maintain unique numbering between types though, but I couldn’t find any examples on the net, short of using ProtoInclude attribute which requires a subtype to be specified.
How would I go about implementing inheritance with protobuf-net like that if I don't know what the subtypes are?
To further expand Marc's answer, specifically dealing with
RuntimeTypeModel
, this is one way to write it:If you have more classes derived from derived classes, link them like this
And so on.
You can then use
Serializer.Serialize(file,object)
, as you would normally with protobuf-net.This works across projects and namespaces.
By adding helper extension method:
You can streamline sub-type registration like this:
If you can't specify the subtypes in attributes (because it isn't known at compile-time) you have 2 options (both of which only apply to "v2", available as beta):
RuntimeTypeModel
, rather than the staticSerializer
methods (which are now just a short-cut toRuntimeTypeModel.Default
); tell the model about the inheritance (example below)DynamicType = true
to the[ProtoMember(...)]
in questionThe second is not very pure protobuf - it embeds type information, which I don't really love but people just kept asking for. The first is my preferred option. To add subtypes at runtime:
the
true
in the above means "use normal rules to add member properties automatically" - you can also take control of that and specify the properties (etc) manually if you prefer.Note that a
TypeModel
should be cached and re-used (not created per object you need to serialize), as it includes some "emit" code to generate methods. Re-using it will be faster and require less memory. The type-model is thread-safe, and can be used to serialize/deserialize multiple streams concurrently on different threads.