![](/img/trans.png)
[英]Can an object serialized with protobuf-net in c# be deserialized with python by generating a .proto file?
[英]Preserving proto comments when generating C# with protobuf-net
我们使用protobuf-net来处理C#应用程序中的协议缓冲区需求。 由于我们将.proto文件与其他非托管应用程序共享,因此我们从.proto文件生成代码(不使用代码优先的protobuf-net方法)。 为了尽可能保持DRY ,我们在.proto文件中保留了大量的界面文档。 我们通过protogen.exe生成C#代码,由项目构建目标调用。
现在,有没有办法(自动)将这些注释转移到已编译的C#代码中?
基本上,给出一个像这样的.proto:
// This message is used to request a resource from the server
message GetResource
{
// The identifier of the requested resource
required string resourceId = 1;
}
...我想要这样的东西(为了便于阅读,省略了IExtensible方法):
/// <summary>
/// This message is used to request a resource from the server
/// </summary>
[global::System.Serializable,global::ProtoBuf.ProtoContract(Name=@"GetResource")]
public partial class GetResource : global::ProtoBuf.IExtensible
{
public GetResource() {}
private string _resourceId;
/// <summary>
/// The identifier of the requested resource
/// [Required] <-- Would be nice...
/// </summary>
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"resourceId",
DataFormat = global::ProtoBuf.DataFormat.Default)]
public string ResourceId
{
get { return _resourceId; }
set { _resourceId = value; }
}
}
实际上当前版本确实支持评论。 它可以使用--include_source_info启用。
注释可在descriptor.Location [n] .leading_comments和trailing_comments中找到: https : //code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto
我已将相应的属性添加到protobuf-net Location类:
private string _leading_comments = "";
[global::ProtoBuf.ProtoMember(3, IsRequired = false, Name = @"leading_comments", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue("")]
public string leading_comments
{
get { return _leading_comments; }
set { _leading_comments = value; }
}
private string _trailing_comments = "";
[global::ProtoBuf.ProtoMember(4, IsRequired = false, Name = @"trailing_comments", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue("")]
public string trailing_comments
{
get { return _trailing_comments; }
set { _trailing_comments = value; }
}
并将--include_source_info添加到protoc调用(ProtoBuf.CodeGenerator.InputFileLoader)
带有注释的位置已添加到生成的xml中:
<?xml version="1.0" encoding="utf-16"?>
<FileDescriptorSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<file>
<FileDescriptorProto>
<name>Test.proto</name>
<dependency />
<message_type>
<DescriptorProto>
<name>Test2</name>
<field>
<FieldDescriptorProto>
<name>IntValue</name>
<number>1</number>
<type>TYPE_INT32</type>
</FieldDescriptorProto>
</field>
<extension />
<nested_type />
<enum_type />
<extension_range />
</DescriptorProto>
</message_type>
<enum_type />
<service />
<extension />
<source_code_info>
<location>
...
<Location>
<path>
<int>4</int>
<int>0</int>
<int>2</int>
<int>0</int>
</path>
<span>
<int>1</int>
<int>0</int>
<int>28</int>
</span>
<trailing_comments> some comment
</trailing_comments>
</Location>
...
</location>
</source_code_info>
</FileDescriptorProto>
</file>
</FileDescriptorSet>
source .proto:
message Test2{
optional int32 IntValue = 1;// some comment
}
但是我在xslt中不强大,无法更新ProtoGen / csharp.xslt以包含对生成的CS文件的注释
目前,我认为答案是“不”。 据我所知,“protoc”(Google用于解析.proto文件的工具,在引擎盖下使用)会默默地丢弃注释 - 因此没有任何内容可供阅读。 如果编写了自定义解析器,那么它是可能的,但是对于哪些注释适用于哪些行,还存在语言歧义,例如:
// this probably relates to resourceId
required string resourceId = 1;
required int foo = 2; // but... is this foo? or bar?
// and what about this?
// what does this relate to? and why?
// and this? what are the rules?
required int bar = 3;
所以出于两个不同的原因:目前,没有。 考虑所有建议,但是......特别是如果它们附带自定义解析器:)
请注意,由于这个原因,大多数(所有?)实现都缺少AFAIK此信息。 不过,我很高兴得到纠正。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.