c# - 无法识别 .proto 文件中的 protobuf-net [默认]?

标签 c# protocol-buffers protobuf-net

我使用 .proto 文件来指定我的 protobuf 消息。我使用 [default=],但看不到设置值的位置。它们不在自动生成的 .cs 文件中。我想在创建消息时设置一些默认值。我无法使用默认构造函数,因为它位于自动生成的 .cs 文件中。

有什么办法解决这个问题吗?

我的 .proto 文件:

package Messages;

message Ack
{
    required bool is_error = 1 [default=false];
    required string message = 2 [default="ok"];
    required string request_id = 3;
}

生成到:

namespace Messages
{
  [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Ack")]
  public partial class Ack : global::ProtoBuf.IExtensible
  {
    public Ack() {}

    private bool _is_error;
    [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"is_error", DataFormat = global::ProtoBuf.DataFormat.Default)]
    public bool is_error
    {
      get { return _is_error; }
      set { _is_error = value; }
    }
    private string _message;
    [global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"message", DataFormat = global::ProtoBuf.DataFormat.Default)]
    public string message
    {
      get { return _message; }
      set { _message = value; }
    }
    private string _request_id;
    [global::ProtoBuf.ProtoMember(3, IsRequired = true, Name=@"request_id", DataFormat = global::ProtoBuf.DataFormat.Default)]
    public string request_id
    {
      get { return _request_id; }
      set { _request_id = value; }
    }
    private global::ProtoBuf.IExtension extensionObject;
    global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
      { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
  }

我使用的是Visual Studio 2013 Update 3 english和最新版的protobuf-net。

最佳答案

如果我举个例子:

 message Foo {
    optional int32 value = 1 [default = 123];
 }

然后通过 protogen 运行它,然后我得到的输出是:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

// Generated from: my.proto
namespace my
{
  [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Foo")]
  public partial class Foo : global::ProtoBuf.IExtensible
  {
    public Foo() {}

    private int _value = (int)123;
    [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"value", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
    [global::System.ComponentModel.DefaultValue((int)123)]
    public int value
    {
      get { return _value; }
      set { _value = value; }
    }
    private global::ProtoBuf.IExtension extensionObject;
    global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
      { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
  }

}

这显然在字段初始值设定项中和通过 DefaultValueAttribute 包含了默认值。

如果我添加 -p:detectMissing 选项,那么我会得到:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

// Option: missing-value detection (*Specified/ShouldSerialize*/Reset*) enabled

// Generated from: my.proto
namespace my
{
  [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Foo")]
  public partial class Foo : global::ProtoBuf.IExtensible
  {
    public Foo() {}

    private int? _value;
    [global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"value", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
    public int value
    {
      get { return _value?? (int)123; }
      set { _value = value; }
    }
    [global::System.Xml.Serialization.XmlIgnore]
    [global::System.ComponentModel.Browsable(false)]
    public bool valueSpecified
    {
      get { return this._value != null; }
      set { if (value == (this._value== null)) this._value = value ? this.value : (int?)null; }
    }
    private bool ShouldSerializevalue() { return valueSpecified; }
    private void Resetvalue() { valueSpecified = false; }

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

}

这现在在“getter”中具有默认值。

关于c# - 无法识别 .proto 文件中的 protobuf-net [默认]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25846033/

相关文章:

c# - 使用 Protobuf-net 和 Monotouch for IOS 通过 WCF 序列化 IEnumerable

c# - Entity Framework 返回两个表连接的 IQueryable

protocol-buffers - 尝试在 Protobuf 中创建 2D 数组时获得预期的顶级语句(例如 "message")

javascript - 如何关联 asp .net 和 KnockoutJS 之间的按钮?

python - key 错误 : Frozen Tensorflow Model to UFF graph

C++ - 谷歌protobuf

c# - 如何使用 Protocol Buffer 的扩展来维护 'general' 消息

没有属性的 Protobuf-net 派生类会导致运行时错误

c# - Blend for Visual Studio 2012 设计 View 不起作用

c# - Oracle 更新表中的重复行