0
protobuf-net proto2 c#
protobuf-net:如何在消息中表示繼承並生成派生類?
這裏是我的.cs
文件,對於protobuf-net
序列化/反序列化工作:
[ProtoBuf.ProtoContract([email protected]"MyBaseTypeProto")]
[ProtoBuf.ProtoInclude(typeof(MyDerivedType), 1)]
public partial class MyBaseType { ... }
[ProtoBuf.ProtoContract([email protected]"MyDerivedTypeProto")]
public partial class MyDerivedType : MyBaseType { ... }
[ProtoBuf.ProtoContract([email protected]"MyMessageProto")]
public partial class MyMessage
{
[ProtoBuf.ProtoMember(1, IsRequired = false, Name = @"MyList", DataFormat = ProtoBuf.DataFormat.Default)]
public List<MyDerivedType> MyList;
是否有可能構成.proto
文件,以便protogen.exe
會產生上面?
是這樣的:
MyBaseType.proto
message MyBaseType {
...
}
MyDerivedType.proto
message MyDerivedType {
...
}
MyMessage.proto
import "MyDerivedType.proto"
message MyMessage{
repeated MyDerivedType MyList = 1;
}
但protogen.exe
施加到上述消息類型根據需要將不生成上述類別;具體地說,它不會產生這些行的代碼:
[ProtoBuf.ProtoInclude(typeof(MyDerivedType), 1)]
public partial class MyDerivedType : MyBaseType
所以'protobuf' spec說支持具有繼承的類結構,'message type defs'應該保留一系列'字段索引'來滿足所有潛在的子分類需求:以這種方式,消息利用組合「內部「來支持」外部「繼承。但是如何克服由於在「sub-class」消息中所有字段都被標記爲平等成員的缺點,'protogen.exe'不能生成正確表示其基類的派生類? – BaltoStar
我覺得你必須適應,而不是克服。以不同的方式思考你的問題,所以它不需要在消息類中繼承。你可能在你自己的類中有繼承,這些類包裝和/或確定如何解析消息。 – asynchronos