Protobuf-net多重派生契约

本文关键字:契约 派生 Protobuf-net | 更新日期: 2023-09-27 18:06:31

我有以下基类:

[ProtoContract]
public class Packet
{
    [ProtoMember(1)]
    public int Id { get; set; }
}

这个类将由10多个其他类派生。我的问题是我应该如何分配ProtoInclude属性?

这是一个好的解决方案吗?

[ProtoContract]
[ProtoInclude(20, typeof(NotifyClientBalance))]
[ProtoInclude(21, typeof(IsAlive))]
[ProtoInclude(22, typeof(TransactionConfirmation))]
...
public class Packet
{
    [ProtoMember(1)]
    public int Id { get; set; }
}

有更好的选择吗?我应该如何分配标签到ProtoInclude?对我来说,一切都不清楚,而且没有关于这件事的好的文档。

感谢

公立小学

ProtoInclude在不同平台上是可移植的吗?我对Python特别感兴趣。

再次感谢:)

Protobuf-net多重派生契约

对于在protobuf-net中使用,这种方法很好。这些数字很好,很低,这使它保持高效。

继承不是protobuf规范的一部分,所以它不能在平台之间移植——至少不能作为继承。它在获取数据方面是便携的。它映射到如下内容:

message Packet {
    optional int id = 1;
    optional NotifyClientBalance balance = 20;
    // ...
}
message NotifyClientBalance {...}
//...

或者直接使用Serializer.GetProto<T>()将模式导出为。proto