.NET – RUNTIMETYPEMODEL How to associate ProtoinClude with the type in the protobuf-net?

As far as I know, RuntimeTypeModel allows ProtoInclude to be associated with a type, which is useful for situations where the type declaration cannot be changed. But I find it difficult to understand how it is actually done.

Is there an example?

Thank you.

AddSubType() is used to specify the derived type and its identifier Symbol; 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 (through attributes) and explicit (through code).

According to me As you know, RuntimeTypeModel allows ProtoInclude to be associated with a type, which is useful for situations where the type declaration cannot be changed. But I find it difficult to understand how it is actually done.

There is an example NS?

Thank you.

AddSubType() is used to specify the derived type and its identifier; for example (full code):

< /p>

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 entire type model is configured at runtime, but you also You can mix and match between automatic (through attributes) and explicit (through code).

Leave a Comment

Your email address will not be published.