7

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

Is there an example?

Thanks.

mark
  • 59,016
  • 79
  • 296
  • 580

1 Answers1

11

AddSubType() is used to specify derived types, along with their identifier; for example (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 above configures the entire type model at runtime, but you can also mix-and-match between automatic (via properties) and explicit (through code).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    +1 @Marc Gravell: then we should use model.Serialize and model.Deserialize from there rather than ProtoBuf.Serializer.De/serialize? Also, does AnotherDerived derive from SomeBase in your example? – sgtz Jul 27 '11 at 15:16
  • @sgtz definite yes, and yes from memory. In that order. – Marc Gravell Jul 27 '11 at 15:45