Protobuf-net.Protogen问题带有自定义选项
顾名思义,我最近一直在尝试将自定义选项添加到我的某些.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论