3

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;
}
Shane
  • 2,271
  • 3
  • 27
  • 55
  • any chance this answers it? https://stackoverflow.com/a/45162348/23354 - and if not: is there a specific bit that is unclear? – Marc Gravell Jul 18 '17 at 10:26
  • I think I've got it, thanks very much for your help. Have up updated my question to show what I think the mapping should look like. If would so kind as verify whether it is correct or not, that would be great. One thing that I wondering - why are the int fields mapped as optional? – Shane Jul 19 '17 at 01:56
  • because protobuf-net defaults to using implicit zero defaults (like proto3) - if they are zero, they aren't included. This can be changed by specifying IsRequired on [ProtoMember]. Looks fine, though. Did you know you can use `Serializer.GetProto`? – Marc Gravell Jul 19 '17 at 05:47

0 Answers0