As the name suggests, I've recently been trying to add custom options to some of my .proto
files. However, it seems like the generated c# code contains a CS1503 error.
Here's an example using the standalone Protogen tool
syntax = "proto3";
import "google/protobuf/descriptor.proto";
extend google.protobuf.FieldOptions {
optional int32 my_custom_int = 50001;
}
message MyMessage {
optional int32 my_int = 1 [(my_custom_int) = 5];
}
And the generated c# result:
// <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
[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(1, Name = @"my_int")]
public int MyInt
{
get => __pbn__MyInt.GetValueOrDefault();
set => __pbn__MyInt = value;
}
public bool ShouldSerializeMyInt() => __pbn__MyInt != null;
public void ResetMyInt() => __pbn__MyInt = null;
private int? __pbn__MyInt;
}
public static partial class Extensions
{
public static int GetMyCustomInt(this global::Google.Protobuf.Reflection.FieldOptions obj)
=> obj == null ? default : global::ProtoBuf.Extensible.GetValue<int>(obj, 50001);
public static void SetMyCustomInt(this global::Google.Protobuf.Reflection.FieldOptions obj, int value)
=> global::ProtoBuf.Extensible.AppendValue<int>(obj, 50001, value);
}
#pragma warning restore CS0612, CS0618, CS1591, CS3021, IDE0079, IDE1006, RCS1036, RCS1057, RCS1085, RCS1192
#endregion
The issue here is that visual studio throws a CS1503 error as Protobuf.Extensible.AppendValue()
expects an IExtensible
, which FieldOptions
isn't.
I'm relatively new to Protobuf and am a bit clueless as to where the issue might be coming from. Did I forget part of the .proto
declaration or are custom options not supported by protobuf-net.protogen
?