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