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

标签 c# inheritance serialization protocol-buffers protobuf-net

对于最新版本的 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/

相关文章:

c# - 尝试实现自定义格式化程序,但从未调用 ICustomFormatter.Format

c# - 使用平铺 View 时在 C# ListView 中显示子项

c++ - C++ 中的协变返回类型到底是什么?

c# - 在 C# 中反序列化自定义 XML 数据类型

c# - 我的域模型中的图像应该使用什么数据类型?

c# - Linq to SQL 扩展方法 : Method overloading, 错误

php - 组织类(class) - 帮助 OOP 初学者

javascript - 如何在Javascript中继承静态和非静态属性?

java - 使用 ReferenceTypeDeserializer 与 Jackson & Spring 反序列化通用类型

java - "Serializable"类的子类自动为 "Serializable"吗?