protobuf-net:如何注释派生类型的属性?
protobuf-net : how to annotate properties of derived type?
对于 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 types
的 xml defs
生成 类 的解决方案。所以理想情况下,这将通过在属性定义上注释的属性来完成。
似乎可以使用 protogen.exe
根据包含 import
语句的消息类型定义(.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 真的不关心那个。
对于 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 types
的 xml defs
生成 类 的解决方案。所以理想情况下,这将通过在属性定义上注释的属性来完成。
似乎可以使用 protogen.exe
根据包含 import
语句的消息类型定义(.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 真的不关心那个。