I've (unsuccessfully) been trying to retrieve custom options I've added to a .proto file in the then generated C# classes (using protobuf-net.protogen) but I don't understand the intended workflow here.
Using Google.Protobuf, you can retrieve the static Descriptor attached to the generated class, and retrieve any associated Descriptor you may need, then retrieve your associated custom extensions from there. However, I've been unable to replicate the same thing using protobuf-net, despite the fact protogen does seem to understand custom options and generate associated Extensions. Here's an example:
syntax = "proto3";
import "google/protobuf/descriptor.proto";
package ProtoGenTest.GeneratedClasses;
extend google.protobuf.FieldOptions {
optional int32 a_custom_option = 50000;
}
message MyMessage {
int32 Id = 12345 [(a_custom_option) = 5];
}
// <auto-generated>
// This file was generated by a tool; you should avoid making direct changes.
// Consider using 'partial classes' to extend these types
// Input: my.proto
// </auto-generated>
#region Designer generated code
#pragma warning disable CS0612, CS0618, CS1591, CS3021, IDE0079, IDE1006, RCS1036, RCS1057, RCS1085, RCS1192
namespace ProtoGenTest.GeneratedClasses
{
[global::ProtoBuf.ProtoContract()]
public partial class MyMessage : global::ProtoBuf.IExtensible
{
private global::ProtoBuf.IExtension __pbn__extensionData;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
=> global::ProtoBuf.Extensible.GetExtensionObject(ref __pbn__extensionData, createIfMissing);
[global::ProtoBuf.ProtoMember(12345)]
public int Id { get; set; }
}
public static partial class Extensions
{
public static int GetACustomOption(this global::Google.Protobuf.Reflection.FieldOptions obj)
=> obj == null ? default : global::ProtoBuf.Extensible.GetValue<int>(obj, 50000);
public static void SetACustomOption(this global::Google.Protobuf.Reflection.FieldOptions obj, int value)
=> global::ProtoBuf.Extensible.AppendValue<int>(obj, 50000, value);
}
}
#pragma warning restore CS0612, CS0618, CS1591, CS3021, IDE0079, IDE1006, RCS1036, RCS1057, RCS1085, RCS1192
#endregion
My issue here is that I'd like to access the GetACustomOption method, but am not sure where I can access the FieldOptions for my generated class. I would be greatly appreciative of anyone showing me the intended workflow to do such an operation.
Thanks a lot for the help!