Rom*_*ier 10 c# code-generation protocol-buffers protobuf-net
我正在使用protobuf-net,我正在尝试:
这分别很容易使用:
protogen.exe 工具Serializer<T>.GetProto() 方法但问题是我需要支持protobuffer 自定义选项,但它似乎并不像我那样简单.
让我解释:
基本上,给出:
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;
}
Run Code Online (Sandbox Code Playgroud)
我想生成:
[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;
}
Run Code Online (Sandbox Code Playgroud)
......反之亦然.
备注:
my_message_option和
my_field_option)已经存在于protofile中(例如,my_custom_options.proto),自定义属性类也可以存在于某个地方(MyMessageOptionAttribute和
MyFieldOptionAttribute).实现这一目标的首选方式是什么?解决方案不必依赖protobuf-net.
我最终从protobuf-csharp分叉了 ProtoGen 项目,以公开一些内部类型(大部分是生成器),并SourceGenerators通过允许自定义生成器注册来使其可扩展。
这样,我就能够使用原型描述符对象模型来编写我自己的 C# 生成器。
一个棘手的问题是在启动解析之前注册您的自定义选项类型:
Protoc.exethen ProtoGen.exe)ExtensionRegistry使用生成的方法进行配置RegisterAllExtensions。Descriptor.Options集合。