【问题标题】:protobuf-net and optional valuesprotobuf-net 和可选值
【发布时间】:2013-06-28 21:46:29
【问题描述】:

我正在尝试从 .proto 文件生成协议缓冲区 C# 代码,不幸的是,protobuf-net 正在生成无法编译的代码。

我的 .proto 中导致问题的具体部分是:

// Counter value response
message RpbCounterGetResp {
    optional sint64 value = 1;
}

当protobuf-net使用命令行工具'C:\Program Files (x86)\protobuf-net\protobuf-net-VS9\protogen.exe' -p:detectMissing -ns:CorrugatedIron.Messages -i:riak_kv.proto -o:riak_kv.cs自动生成时

我得到以下块 o' 不可编译的代码:

[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"RpbCounterUpdateResp")]
public partial class RpbCounterUpdateResp : global::ProtoBuf.IExtensible, global::System.ComponentModel.INotifyPropertyChanging
{
  public RpbCounterUpdateResp() {}  

  private long? _value;
  [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"value", DataFormat = global::ProtoBuf.DataFormat.ZigZag)]
  public long value
  {
    get { return _value?? default(long); }
    set { OnPropertyChanging(@"value"); _value = value; }
  }
  [global::System.Xml.Serialization.XmlIgnore]
  [global::System.ComponentModel.Browsable(false)]
  public bool valueSpecified
  {
    get { return _value != null; }
    /* this is the big that won't compile as there's no conversion 
       between bool and System.Nullable<long>
     */ 
    set { if (value == (_value== null)) _value = value ? value : (long?)null; }
  }
  private bool ShouldSerializevalue() { return valueSpecified; }
  private void Resetvalue() { valueSpecified = false; }

  public event global::System.ComponentModel.PropertyChangingEventHandler PropertyChanging;
  protected virtual void OnPropertyChanging(string propertyName)
  { if(PropertyChanging != null) PropertyChanging(this, new global::System.ComponentModel.PropertyChangingEventArgs(propertyName)); }

  private global::ProtoBuf.IExtension extensionObject;
  global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
    { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}

是否可以提供额外的 protogen 选项来解决此问题?

【问题讨论】:

  • 这很烦人;那里有代码可以尝试发现关键字并添加 @ 前缀 - 我想我错过了一个;我需要解决这个问题

标签: c# protobuf-net


【解决方案1】:

原来这是由名为 value 的 .proto 属性之间的 C# 保留关键字冲突引起的,这会产生无法编译的代码。将自动生成的属性名称更改为 returnValue 可以解决问题……直到下一次自动生成代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多