使用protobuf-net生成C#时保留proto注释
我们使用它来处理C#应用程序中的协议缓冲区需求。由于我们与其他非托管应用程序共享.proto文件,因此我们从.proto文件生成代码(而不是使用代码优先protobuf-net方法)。为了尽可能地保留,我们在.proto文件中保存了大量的接口文档。我们通过protogen.exe生成C#代码,protogen.exe由项目构建目标调用 现在,有没有办法(自动)将这些注释转换成编译后的C#代码 基本上,给定这样的.proto:使用protobuf-net生成C#时保留proto注释,c#,.net,protocol-buffers,protobuf-net,documentation-generation,C#,.net,Protocol Buffers,Protobuf Net,Documentation Generation,我们使用它来处理C#应用程序中的协议缓冲区需求。由于我们与其他非托管应用程序共享.proto文件,因此我们从.proto文件生成代码(而不是使用代码优先protobuf-net方法)。为了尽可能地保留,我们在.proto文件中保存了大量的接口文档。我们通过protogen.exe生成C#代码,protogen.exe由项目构建目标调用 现在,有没有办法(自动)将这些注释转换成编译后的C#代码 基本上,给定这样的.proto: // This message is used to request
// This message is used to request a resource from the server
message GetResource
{
// The identifier of the requested resource
required string resourceId = 1;
}
…我想要这样的东西(为了可读性,省略了扩展方法):
//
///此消息用于从服务器请求资源
///
[全局::System.Serializable,全局::ProtoBuf.ProtoContract(名称=@“GetResource”)]
公共部分类GetResource:global::ProtoBuf.IEExtensible
{
公共GetResource(){}
私有字符串_resourceId;
///
///请求的资源的标识符
///[必选]目前,我相信答案是“否”。据我所知,“protoc”(谷歌用于解析.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;
因此,有两个不同的原因:目前没有。不过,所有的建议都经过了考虑……特别是如果它们附带了自定义解析器:)
请注意,由于这个原因,大多数(所有?)实现中都缺少此信息。不过,我很高兴得到更正。实际上当前版本确实支持注释。它可能通过--include\u source\u info启用
注释在描述符中可用。位置[n]。前导注释和尾随注释:
我已将相应的属性添加到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; }
}
并添加--将源信息包含到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>
但是我不擅长xslt来更新ProtoGen/csharp.xslt以将注释包含到生成的CS文件中语言歧义可以通过定义一个约定并坚持它来解决。我想应该有人向protoc发出功能请求,在输出中包含注释,除非它已经在列表中。自定义解析器都是anno我很想写,而且我看到的大多数都没有完全实现.proto语法。为protobuf net问题racker添加了功能请求。很有趣,谢谢!我希望这将在protobuf net中实现。这将是一个伟大的补充。据我所知,这现在与proto 3.0.0中的protoc
一起工作。Itz适用于C#和java,但不是C++的AfikTies,最后!工作就像一个魅力。
<?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>
message Test2{
optional int32 IntValue = 1;// some comment
}