How can RuntimeTypeModel be used to bind ProtoInclude to a type in protobuf-net?

As I understand it, RuntimeTypeModel allows you to associate ProtoInclude with a type, which is useful for cases when the type declaration cannot be changed. But it’s hard for me to understand how this is actually done.

Is there an example?

Thanks.

+5
source share
1 answer

AddSubType() used to specify derived types along with their identifier; e.g. ( full code ):

  static RuntimeTypeModel CreateModel() { var model = TypeModel.Create(); model[typeof(NotInvolved)].Add(1, "D"); model[typeof(SomeBase)] .Add(1, "A") .AddSubType(2, typeof(SomeDerived)) .AddSubType(3, typeof(AnotherDerived)); model[typeof(SomeDerived)].Add(1, "B"); model[typeof(AnotherDerived)].Add(1, "C"); model[typeof(AlsoNotInvolved)].Add(1, "E"); return model; } 

The configuration of the model above is of the entire type at runtime, but you can also mix and match between automatic (through properties) and explicit (through code).

+10
source

All Articles