Protobuf-net.Protogen问题带有自定义选项

发布于 2025-02-12 18:45:37 字数 2319 浏览 0 评论 0原文

顾名思义,我最近一直在尝试将自定义选项添加到我的某些.proto文件中。但是,似乎生成的C#代码包含CS1503错误。

这是使用独立的原始工具

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];
}

和生成的C#结果的一个示例:

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

这里的问题是,Visual Studio将CS1503错误作为ProtoBuf.extensible.AppendValue()期望iextensible ,哪个fieldOptions不是。

我对Protobuf的新手相对较新,对于问题的来源而言,我有点不知所措。我是否忘记了.proto声明的一部分,还是protobuf-net.protogen不支持自定义选项?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文