How can I model inheritance in protobuf-net?

Viewed 1213

I have been reading the protobuf-net documentation but am still unsure how to express inheritance when there are multiple classes. Based on the answer mentioned in Marc's comment bellow I have created the following sample mapping followed by what I understand the equivalent .proto syntax would look like. I understand that by mapping this way I will have room for up to 99 members in class which is inherited form. Are my assumptions correct?

[ProtoContract]
[ProtoInclude(100, typeof(Class2))]
[ProtoInclude(101, typeof(Class3))]
class Class1 {
    [ProtoMember(1)]
    int field1;
}

[ProtoContract]
[ProtoInclude(100, typeof(Class4))]
class Class2 : Class1 {
    [ProtoMember(1)]
    int field2;
}

class Class3 : Class1 {
    [ProtoMember(1)]
    int field2;
}

class Class4 : Class2 {
    [ProtoMember(1)]
    int field3;
}

equivalent .proto syntax:

message Class1 {
    optional int32 field1 = 1;
    optional Class2 _notNamed = 100;
    optional Class3 _notNamed = 101;
}

message Class2 {
    optional int32 field2 = 1;
    optional Class4 _notNamed = 100;
}

message Class3 {
    optional int32 field2 = 1;
}

message Class4 {
    optional int32 field3 = 1;
}
0 Answers
Related