gpt4 book ai didi

protobuf-net - 如何为我的 protobuf-net 类获取基类

转载 作者:行者123 更新时间:2023-12-01 02:32:00 28 4
gpt4 key购买 nike

我已经阅读了有关继承的各种帖子,并且 Protocol Buffer 不支持继承。我不想继承 Protocol Buffers 消息,而是继承,这样我就可以轻松处理我的所有 Protocol Buffers 消息。

我正在使用 protobuf-net 2.0.0.480 和 .proto 文件来定义我的协议(protocol)。一切都很好,除非我想要一个共同的祖先,这样我就可以做一些共同的功能并便于检查。一个简单的例子:

我的 .proto 文件:

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

它最终为 GFX_Layout_Advance、GFX_Layout_Render 生成类,如下所示(仅 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

因为它是一个部分类,并且我实现了似乎没有可覆盖的构造函数:
public partial class GFX_Layout_Advance : GfxProtocolMessageBase
{
public override ProtocolInformation ProtocolInformation()
{
return ProtocolInfo;
}
}

这将允许我将所有传入消息视为 GfxProtocolMessageBase 并允许查询 ProtocolInformation 以便我可以转换为适当的后代。在这种情况下,GFX_Layout_Advance。然而.....
  • 添加额外的部分类 GFX_Layout_Advance() 会导致不同的 protobuf 编码。由于界面的唯一变化是一种我不明白为什么会这样的方法?

  • 底线是:
  • 我想为所有生成的 protobuf-net 类引入一个共同的基础祖先
  • 基本祖先类将允许我访问有关我正在处理的消息类型的信息,因为在我准备好之前我不想强制转换为实际的消息类型

  • 我如何实现 1. 和 2.?

    所有指针表示赞赏。

    最佳答案

  • 是的,只要 GfxProtocolMessageBase 不是合约类型,它就可以正常工作。它故意使用部分类来允许这种类型的事情。编码数据不应改变。如果你有一个我可以看到的行为不端的场景,我会很乐意调查。
  • 没关系;只是:不要使用 Serializer.Serialize<GfxProtocolMessageBase>/Serializer.Deserialize<GfxProtocolMessageBase> ,因为序列化程序不应该知道 GfxProtocolMessageBase (除非您当然乐意,但这确实意味着您不会 100% 遵循现有的 .proto)。对于序列化,Serializer.NonGeneric.SerializetypeModel.Serialize(例如 RuntimeTypeModel.Default.Serialize )将自动执行正确的操作。对于反序列化,您需要知道实际的目标 Type

  • 当然,另一种选择是允许序列化程序将 GfxProtocolMessageBase 作为基本类型知道,并使用 protobuf-net 的内置继承支持( [ProtoInclude(...)] 等) - 但问题是:这不会 100% 映射到您的.proto,因为继承是(通过 protobuf-net)作为封装实现的,这意味着:它将被写成一个带有许多可选子消息字段的基本消息。

    编辑以显示用于从单个流中读取不同对象(异构类型)的类型解析器用法:
    using ProtoBuf;
    using System;
    using System.Collections.Generic;
    using System.IO;
    [ProtoContract]
    class Foo
    {
    [ProtoMember(1)]
    public int Id { get; set; }

    public override string ToString()
    {
    return "Foo with Id=" + Id;
    }
    }
    [ProtoContract]
    class Bar
    {
    [ProtoMember(2)]
    public string Name { get; set; }

    public override string ToString()
    {
    return "Bar with Name=" + Name;
    }
    }
    static class Program
    {
    // mechanism to obtain a Type from a numeric key
    static readonly Dictionary<int, Type> typeMap = new Dictionary<int, Type>
    {
    {1,typeof(Foo)}, {2,typeof(Bar)}
    };
    static Type ResolveType(int key)
    {
    Type type;
    typeMap.TryGetValue(key, out type);
    return type;
    }
    static void Main()
    {
    // using MemoryStream purely for convenience
    using (var ms = new MemoryStream())
    {
    // serialize some random data (here I'm coding the outbound key
    // directly, but this could be automated)
    Serializer.SerializeWithLengthPrefix(ms, new Foo { Id = 123 },
    PrefixStyle.Base128, 1);
    Serializer.SerializeWithLengthPrefix(ms, new Bar { Name = "abc" },
    PrefixStyle.Base128, 2);
    Serializer.SerializeWithLengthPrefix(ms, new Foo { Id = 456 },
    PrefixStyle.Base128, 1);
    Serializer.SerializeWithLengthPrefix(ms, new Bar { Name = "def" },
    PrefixStyle.Base128, 2);

    // rewind (this wouldn't be necessary for a NetworkStream,
    // FileStream, etc)
    ms.Position = 0;

    // walk forwards through the top-level data
    object obj;
    while (Serializer.NonGeneric.TryDeserializeWithLengthPrefix(
    ms, PrefixStyle.Base128, ResolveType, out obj))
    {
    // note we overrode the ToString on each object to make
    // this bit work
    Console.WriteLine(obj);
    }
    }
    }
    }

    关于protobuf-net - 如何为我的 protobuf-net 类获取基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12577392/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com