gpt4 book ai didi

c# - protobuf 网络 : how to annotate properties of derived type?

转载 作者:太空宇宙 更新时间:2023-11-03 12:38:59 25 4
gpt4 key购买 nike

对于最新版本的 protobuf-net(r640 文件夹),如何最好地注释派生类型的 ProtoMember

[ProtoBuf.ProtoContract(Name=@"MyBaseTypeProto")]                                       
[Serializable]
public partial class MyBaseType: ProtoBuf.IExtensible { ... }

[ProtoBuf.ProtoContract(Name=@"MyDerivedTypeProto")]
[Serializable]
public partial class MyDerivedType : MyBaseType, ProtoBuf.IExtensible { ... }

[ProtoBuf.ProtoContract(Name=@"MyMessageProto")]
[Serializable]
public partial class MyMessage : ProtoBuf.IExtensible
{
[ProtoBuf.ProtoMember(1, IsRequired = false, Name = @"MyList", DataFormat = ProtoBuf.DataFormat.Default)]
[System.ComponentModel.DefaultValue(null)]
public List<MyDerivedType> MyList;

我已尝试将 DynamicType 属性添加到 ProtoMember 属性,但无法识别。

我需要一个可以从 proto typesxml defs 生成类的解决方案。因此,理想情况下,这将通过在属性定义上注释的属性来完成。

似乎可以使用 protogen.exe 生成基于包含 import 的消息类型 def(.proto 文件)的类声明:

package MyPackage;                                                          

import "MyDerivedTypeProto.proto";

message MyMessage{
repeated MyDerivedType MyList = 1;
}

import 语句显然对生成的 C# 类(.cs 文件)没有影响,除了添加注释:

// Generated from: MyMessageProto.proto
// Note: requires additional types generated from: MyDerivedType.proto

最佳答案

[ProtoBuf.ProtoContract(Name=@"MyBaseTypeProto")]
[ProtoBuf.ProtoInclude(typeof(MyDerivedType), someFieldNumberUniqueInsideMyBaseType)]
public partial class MyBaseType: ProtoBuf.IExtensible { ... }

[ProtoBuf.ProtoContract(Name=@"MyDerivedTypeProto")] { ... }
public partial class MyDerivedType : MyBaseType, ProtoBuf.IExtensible

[ProtoBuf.ProtoContract(Name=@"MyMessageProto")]
public partial class MyMessage : ProtoBuf.IExtensible
{
[ProtoBuf.ProtoMember(1, IsRequired = false, Name = @"MyList", DataFormat = ProtoBuf.DataFormat.Default)]
[System.ComponentModel.DefaultValue(null)]
public List<MyDerivedType> MyList;

应该这样做(未经测试,不是通过合适的计算机)。关键的添加是基类型上的 [ProtoInclude]。我删除了 [Serializable] 因为 protobuf-net 真的不关心这个。

关于c# - protobuf 网络 : how to annotate properties of derived type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39781871/

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