【问题标题】:Adding a variable to the serialized buffer without serializing it with protobuf.net将变量添加到序列化缓冲区而不使用 protobuf.net 对其进行序列化
【发布时间】:2017-11-13 00:32:57
【问题描述】:

所以我有一个要序列化的类,除了某个值“键” 将用于识别要反序列化它的类。

现在我明白了,这可以手动完成,但我创建了一个处理序列化/反序列化的类,我想让它尽可能通用。

示例代码

[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;
}

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

【问题讨论】:

    标签: c# protobuf-net


    【解决方案1】:

    听起来你想要一个“鉴别器”。 Protobuf-net 目前没有任何代码来支持它,尤其是在反序列化方面(只需编写额外的值相当容易:读取时很难使用它)。 protobuf-net 支持的是:

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

    如果有一个很好的用例来支持更强的鉴别器支持,我很乐意考虑它,但今天不存在。我需要查看示例预期用法并考虑实施后果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 2010-11-25
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      相关资源
      最近更新 更多