I'm using protobuf-net , and I'm trying to:
That's pretty easy using respectively:
protogen.exe
tool Serializer<T>.GetProto()
method But the thing is that I need to support protobuffer custom options and it doesn't seem to be as straightforward as I though.
Let me explain:
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 :
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
). What's the preferred way to achieve that? The solution doesn't have to rely on 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.
This way, I was able to use the proto descriptor object model to write my own C# generator.
One tricky point is to register your custom options types before launching the parsing :
Protoc.exe
then ProtoGen.exe
) ExtensionRegistry
by using the generated RegisterAllExtensions
methods. Descriptor.Options
collection. There isn't a simple way to do this at the moment. The changes required would primarily be to the csharp.xslt file. There is a bug reported about this but hasn't been fixed yet.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.