I read various messages related to inheritance, and that protocol buffers do not support inheritance. I do not want inheritance in protocol buffer messages, but rather inheritance, so I can easily process all messages with protocol buffers.
I am using protobuf-net 2.0.0.480 and the .proto file to determine my protocol. All this works well, except when I get to the point where I need a common ancestor so that I can perform some common functions and allow for easy inspection. A simple example:
My .proto file:
message ProtocolInformation { enum MessageKinds { LAYOUT_ADVANCE = 1; LAYOUT_RENDER = 2; } required MessageKinds MessageKind = 1; required int32 UniqueID = 2; } message GFX_Layout_Advance { required ProtocolInformation ProtocolInfo = 1; required int32 LayoutHandle = 2; } message GFX_Layout_Render { required ProtocolInformation ProtocolInfo = 1; required int32 LayoutHandle = 2; required int32 Stage = 3; }
which ultimately generates classes for GFX_Layout_Advance, GFX_Layout_Render as (only part of GFX_Layout_Advance):
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name = @"GFX_Layout_Advance")] public partial class GFX_Layout_Advance : global::ProtoBuf.IExtensible { public GFX_Layout_Advance() { } private GFX_Protocol.ProtocolInformation _ProtocolInfo; [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name = @"ProtocolInfo", DataFormat = global::ProtoBuf.DataFormat.Default)] public GFX_Protocol.ProtocolInformation ProtocolInfo
Since it was a partial class, and there seemed to be no redefinable constructor that I implemented:
public partial class GFX_Layout_Advance : GfxProtocolMessageBase { public override ProtocolInformation ProtocolInformation() { return ProtocolInfo; } }
This will allow me to process all incoming messages as GfxProtocolMessageBase and allow the protocol request so that I can use the corresponding child. In this case, GFX_Layout_Advance. But.....
- Adding an additional partial class GFX_Layout_Advance () leads to a different encoding of the protobuff. Since the only interface change is the method, I do not understand why this is?
Bottom line:
- I want to introduce a common base ancestor to all the generated protobuf-net classes.
- The base ancestor class will allow me to access information about which message I'm dealing with, because I don't want me to need to pass the actual message type until I'm ready.
How to reach 1. and 2 ..
All pointers appreciated.
source share