c# - 使用 protobuf-net 生成 C# 时保留 proto 注释

标签 c# .net protocol-buffers protobuf-net documentation-generation

我们正在使用 protobuf-net来处理我们在 C# 应用程序中的 Protocol Buffer 需求。由于我们与其他非托管应用程序共享我们的 .proto 文件,因此我们从 .proto 文件生成我们的代码(不使用代码优先的 protobuf-net 方法)。为了保持DRY我们尽可能在 .proto 文件中保留大量接口(interface)文档。我们通过项目构建目标调用的 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 添加到协议(protocol)调用 (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>

源.proto:

message Test2{
optional int32 IntValue = 1;// some comment
}

但我不擅长 xslt 来更新 ProtoGen/csharp.xslt 以将注释包含到生成的 CS 文件中

关于c# - 使用 protobuf-net 生成 C# 时保留 proto 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14830900/

相关文章:

c# - 异常(HRESULT : 0x800AC472) when using Excel. 工作表。调用 Excel.Workbook.SaveAs 后选择

c# - 有没有办法避免注释属性中的魔数(Magic Number)?

c# - 用于在运行时从接口(interface)生成具体类型的开源库?

java - Protocol Buffer : How to define Date type?

java - Riak java客户端中,如何正确处理未找到键/值对象的情况

c# - 我可以在不使用 pinvoke 的情况下在 C# 中获取系统颜色的 RGB 吗?

c# - 使用 NHibernate 在 MVC3 中处理异常和回滚

c# - 通过 Windows 服务启动和停止并发线程

c# - 如何检测 Protocol Buffer 消息何时被完全接收?

c# - 在 C# 中反格式化货币