【问题标题】:Is it possible to generate C# files using protobuf-net for proto2 files with extension of 'FieldOptions'?是否可以使用 protobuf-net 为扩展名为“FieldOptions”的 proto2 文件生成 C# 文件?
【发布时间】:2021-02-16 14:57:14
【问题描述】:

我有一个带有 proto2 语法的 proto 文件。我正在尝试使用 protogen 自动生成 C# 文件。虽然我可以毫无错误地生成文件,但在构建项目时出现错误(C# 文件是 Visual Studio 中 .Net 5 项目的一部分)。错误看起来像:

错误 CS0400:在全局命名空间中找不到类型或命名空间名称“Google”(您是否缺少程序集引用?)

我已将 'descriptor.proto' 添加到正确的路径中,但它似乎对 protogen 自动生成 C# 文件的方式没有任何影响。

myproto.proto

syntax = "proto2";

import "google/protobuf/descriptor.proto";

package test;

extend google.protobuf.FieldOptions {
  optional string sampleValue = 50004;
}

message TestMessage {
  required string id = 1;
  optional float value = 2;
  optional string sample_val = 3;
}

myproto.cs

// <auto-generated>
//   This file was generated by a tool; you should avoid making direct changes.
//   Consider using 'partial classes' to extend these types
//   Input: myproto.proto
// </auto-generated>

#region Designer generated code
#pragma warning disable CS0612, CS0618, CS1591, CS3021, IDE0079, IDE1006, RCS1036, RCS1057, RCS1085, RCS1192
namespace Test
{

    [global::ProtoBuf.ProtoContract()]
    public partial class TestMessage : 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 = @"id", IsRequired = true)]
        public string Id { get; set; }

        [global::ProtoBuf.ProtoMember(2, Name = @"value")]
        public float Value
        {
            get => __pbn__Value.GetValueOrDefault();
            set => __pbn__Value = value;
        }
        public bool ShouldSerializeValue() => __pbn__Value != null;
        public void ResetValue() => __pbn__Value = null;
        private float? __pbn__Value;

        [global::ProtoBuf.ProtoMember(3, Name = @"sample_val")]
        [global::System.ComponentModel.DefaultValue("")]
        public string SampleVal
        {
            get => __pbn__SampleVal ?? "";
            set => __pbn__SampleVal = value;
        }
        public bool ShouldSerializeSampleVal() => __pbn__SampleVal != null;
        public void ResetSampleVal() => __pbn__SampleVal = null;
        private string __pbn__SampleVal;

    }

    public static partial class Extensions
    {
        public static string GetsampleValue(this global::Google.Protobuf.Reflection.FieldOptions obj)
            => obj == null ? default : global::ProtoBuf.Extensible.GetValue<string>(obj, 50004);

        public static void SetsampleValue(this global::Google.Protobuf.Reflection.FieldOptions obj, string value)
            => global::ProtoBuf.Extensible.AppendValue<string>(obj, 50004, value);

    }
}

#pragma warning restore CS0612, CS0618, CS1591, CS3021, IDE0079, IDE1006, RCS1036, RCS1057, RCS1085, RCS1192
#endregion

在生成的“myproto.cs”中的两个方法的静态类Extensions 中发生错误。难道不能只使用 protobuf-net 包而不使用任何额外的 Google 包吗?如果不是,那么我必须添加哪个包作为附加依赖项?我可能遗漏了一些微不足道的东西,但我现在似乎无法理解。

【问题讨论】:

    标签: c# protocol-buffers protobuf-net protogen


    【解决方案1】:

    这里不需要谷歌参考;您有扩展 FieldOptions,这是与字段相关的描述符元数据类型。 protobuf-net 在 protobuf-net.Reflection 包中有一个实现,这正是可以预期的地方:Google.Protobuf.Reflection.FieldOptions。

    但是!我怀疑这是否会达到您的预期。在这种情况下,您已经定义了一个“自定义选项”。自定义选项与运行时实例数据无关 - 它们是 .proto DSL 的一部分,通常用于影响代码生成;在你的情况下,它会是这样的:

      optional string sample_val = 3 [sampleValue= "ExtensionTypeName"];
    

    (或类似的东西)

    这里的重点是 value ("ExtensionTypeName") 仅在谈论 schema 字段 时适用,这就是为什么它与 FieldOptions (它表示模式字段定义的实例)。它与您在运行时的 TestMessage 使用无关。此外:protobuf-net 当前不将自定义选项打包到生成的 C# 中(它可以这样做,大概是作为属性中的 base-64 块,或类似的 - 它目前没有)。

    所以:退后一步:您的扩展名sampleValue 对您意味着什么,您认为它与哪些实例相关?

    【讨论】:

    • 啊反射包是我缺少的那个。谢谢!现在应该可以了。关于扩展和自定义选项,我觉得有点困惑,所以在继续之前我会先做更多的研究。
    猜你喜欢
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    相关资源
    最近更新 更多