By Romain Verdier


2011-03-28 11:43:54 8 Comments

I'm using protobuf-net, and I'm trying to:

  1. Generate a C# class from a .proto file
  2. Generate a .proto file from a C# class

That's pretty easy using respectively:

  1. protogen.exe tool
  2. 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:

  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.
  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.

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).
  • I tried to use protogen.exe and a custom xslt but protogen doesn't seem to have support for custom options.

What's the preferred way to achieve that? The solution doesn't have to rely on protobuf-net.

2 comments

@Marc Gravell 2011-03-28 12:02:24

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.

@Romain Verdier 2011-03-28 12:29:29

I already tried to create a specific xslt file, but I can't have access to the custom options used in the proto file. Can you think of another solution non protobuf-net based?

@Marc Gravell 2011-03-28 12:31:40

@Romain this isn't a configuration I've had chance to look at, so: not off the top of my head, no. If you run it through the xml template (-t:xml or something similar), it will show you what it knows, which might indicate where this data is lurking - assuming it doesn't need an update to the descriptors.

@Romain Verdier 2011-03-28 12:37:18

Yep, that's what I tried (-t:xml) but all custom options appear as uninterpreted_option in the resulting xml file.

@Romain Verdier 2011-03-31 13:31:06

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 :

  1. Generate C# types corresponding to your custom options (using Protoc.exe then ProtoGen.exe)
  2. Configure ExtensionRegistry by using the generated RegisterAllExtensions methods.
  3. Start the code generation using your own C# generators. In each generator you can access to the contextual Descriptor.Options collection.

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] Why not inherit from List&amp;lt;T&amp;gt;?

1 Answered Questions

[SOLVED] generate proto file from xsd

2 Answered Questions

1 Answered Questions

[SOLVED] Protobuf-net .proto file generation for inheritance

1 Answered Questions

[SOLVED] How to generate struct with ProtoGen from proto file

  • 2013-08-21 13:03:41
  • Ondrej Rozinek
  • 776 View
  • 3 Score
  • 1 Answer
  • Tags:   c# .net protobuf-net

1 Answered Questions

[SOLVED] Way to automate GetProto on all types with ProtoContract attribute

1 Answered Questions

[SOLVED] In Protobuf-net, is it possible to serialize non-decorated classes?

  • 2011-06-25 13:17:53
  • sgtz
  • 1945 View
  • 5 Score
  • 1 Answer
  • Tags:   c# protobuf-net

2 Answered Questions

Sponsored Content