protobuf-net:如何注释派生类型的属性? [英] protobuf-net : how to annotate properties of derived type?
问题描述
对于最新版本的protobuf-net
(r640
文件夹),如何最好地注释作为派生类型的ProtoMember
?
For the latest version of protobuf-net
( r640
folder ), how to best annotate a ProtoMember
that is a derived type ?
[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
属性,但是无法识别.
I've tried adding DynamicType
property to the ProtoMember
attribute but it's not recognized.
我需要一个可以从proto types
的xml defs
生成类的解决方案.因此,理想情况下,这可以通过注释属性定义的属性来完成.
I require a solution where the classes can be generated from xml defs
of proto types
. So ideally this would be done via attributes annotated onto properties definitions.
使用protogen.exe
似乎可以基于包含import
语句的消息类型def(.proto
文件)生成类:
It would seem possible to use protogen.exe
to generate classes based on message-type defs ( .proto
files ) that include import
statements :
package MyPackage;
import "MyDerivedTypeProto.proto";
message MyMessage{
repeated MyDerivedType MyList = 1;
}
,但是import
语句显然对生成的C#类(.cs
文件)没有影响,除了添加注释:
but the import
statements apparently have no effect on the generated C# classes ( .cs
files ) except to add a comment :
// 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确实不在乎.
Should do it (untested, not by a suitable computer). The key addition is the [ProtoInclude]
on the base-type. I removed [Serializable]
because protobuf-net really doesn't care about that.
这篇关于protobuf-net:如何注释派生类型的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!