protobuf-net : how to annotate properties of derived type?

一笑奈何 提交于 2020-01-04 05:44:13

问题


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;  

I've tried adding DynamicType property to the ProtoMember attribute but it's not recognized.

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.

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

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

回答1:


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

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.



来源:https://stackoverflow.com/questions/39781871/protobuf-net-how-to-annotate-properties-of-derived-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!