将变量添加到序列化缓冲区而不用 protobuf.net 序列化它

Adding a variable to the serialized buffer without serializing it with protobuf.net

所以我有一个 class 我想序列化,除了某个值 "key" 将用于识别要反序列化的 class。

现在我明白了,这可以手动完成,但我制作了一个 class 来处理 serialization/deserialization,我想让它尽可能通用。

示例代码

[ProtoContract]
public class GeneralPacket 
{
   public uint Identity;
   [ProtoMember(1, IsRequired = true)]
   public uint Arg1;
   [ProtoMember(2, IsRequired = true)]
   public uint Arg2;
   [ProtoMember(3, IsRequired = true)]
   public uint Arg3;
}

我希望 Identity 包含在流中但不序列化。

听起来你想要一个 "discriminator"。 Protobuf-net 目前没有任何代码来支持它,尤其是在反序列化方面(简单地编写额外的值是相当容易的:困难的是在读取时使用它)。 protobuf-net 支持的是:

  • 具有共同祖先和键控子类型的各种继承方案
  • 通过 SerializeWithLengthPrefix 手动使用允许在序列化时指定数字键,并在反序列化时提供类型解析器(从键到类型)

如果有更好的用例来支持更强大的鉴别器,我很乐意考虑,但目前还不存在。我需要查看预期用途示例并考虑实施后果。