简体   繁体   English

从proto文件生成C#,反之亦然解释自定义选项

[英]Generate C# from proto files and vice versa interpreting custom options

I'm using protobuf-net , and I'm trying to: 我正在使用protobuf-net ,我正在尝试:

  1. Generate a C# class from a .proto file 从.proto文件生成C#类
  2. Generate a .proto file from a C# class 从C#类生成.proto文件

That's pretty easy using respectively: 这分别很容易使用:

  1. protogen.exe tool protogen.exe工具
  2. Serializer<T>.GetProto() method Serializer<T>.GetProto()方法

But the thing is that I need to support protobuffer custom options and it doesn't seem to be as straightforward as I though. 但问题是我需要支持protobuffer 自定义选项 ,但它似乎并不像我那样简单。

Let me explain: 让我解释:

  1. From a proto file containing custom options applied on messages and fields, I want to generate a C# class decorated by .NET custom attributes. 从包含应用于消息和字段的自定义选项的proto文件,我想生成一个由.NET自定义属性修饰的C#类。
  2. From a C# class decorated by custom attributes, I would like to generate a proto file where custom options are applied on messages and fields. 在自定义属性修饰的C#类中,我想生成一个proto文件,其中自定义选项应用于消息和字段。

Basically, given: 基本上,给出:

message person {
   option (my_message_option) = true;

   optional string firstname = 1 [(my_field_option) = 42];
   optional string lastname = 2 [(my_field_option) = 12];
   optional int age = 3;
}

I want to generate: 我想生成:

[ProtoContract, MyMessageOption(true)]
public class Person
{
    [ProtoMember(1), MyFieldOption(42)]
    public string Firstname;

    [ProtoMember(2), MyFieldOption(12)]
    public string Firstname;

    [ProtoMember(3)]
    public string Firstname;
}

...and vice versa. ......反之亦然。

Notes : 备注:

  • The custom option definitions ( my_message_option and my_field_option ) can already exist in a protofile (say, my_custom_options.proto), and the custom attributes classes can also exist somewhere ( MyMessageOptionAttribute and MyFieldOptionAttribute ). 自定义选项定义( my_message_optionmy_field_option )已经存在于protofile中(例如,my_custom_options.proto),自定义属性类也可以存在于某个位置( MyMessageOptionAttributeMyFieldOptionAttribute )。
  • I tried to use protogen.exe and a custom xslt but protogen doesn't seem to have support for custom options. 我尝试使用protogen.exe和自定义xslt,但protogen似乎不支持自定义选项。

What's the preferred way to achieve that? 实现这一目标的首选方式是什么? The solution doesn't have to rely on protobuf-net. 解决方案不必依赖protobuf-net。

I ended up by forking ProtoGen project from protobuf-csharp to make public some internal types (generators, for the most part) and to make the SourceGenerators extensible by allowing custom generator registrations. 我最后通过从protobuf-csharp分配ProtoGen项目来公开一些内部类型(生成器,大部分),并通过允许自定义生成器注册使SourceGenerators可扩展。

This way, I was able to use the proto descriptor object model to write my own C# generator. 这样,我就可以使用proto描述符对象模型来编写自己的C#生成器。

One tricky point is to register your custom options types before launching the parsing : 一个棘手的问题是在启动解析之前注册自定义选项类型:

  1. Generate C# types corresponding to your custom options (using Protoc.exe then ProtoGen.exe ) 生成与您的自定义选项对应的C#类型(使用Protoc.exe然后是ProtoGen.exe
  2. Configure ExtensionRegistry by using the generated RegisterAllExtensions methods. 使用生成的RegisterAllExtensions方法配置ExtensionRegistry
  3. Start the code generation using your own C# generators. 使用您自己的C#生成器启动代码生成。 In each generator you can access to the contextual Descriptor.Options collection. 在每个生成器中,您可以访问上下文Descriptor.Options集合。

There isn't a simple way to do this at the moment. 目前没有一种简单的方法可以做到这一点。 The changes required would primarily be to the csharp.xslt file. 所需的更改主要是csharp.xslt文件。 There is a bug reported about this but hasn't been fixed yet. 有关此报告的错误但尚未修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM