Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法识别.proto文件中的protobuf net[默认值]?_C#_Protocol Buffers_Protobuf Net - Fatal编程技术网

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

C# 无法识别.proto文件中的protobuf net[默认值]?,c#,protocol-buffers,protobuf-net,C#,Protocol Buffers,Protobuf Net,我使用.proto文件指定protobuf消息。我使用[default=],但我看不到在哪里设置值。它们不在自动生成的.cs文件中。我想在创建消息时设置一些默认值。我无法使用默认构造函数,因为它位于自动生成的.cs文件中 有没有办法解决这个问题 My.proto文件: package Messages; message Ack { required bool is_error = 1 [default=false]; required string message = 2 [d

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

有没有办法解决这个问题

My.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); }
  }

}

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

请看我上面的添加内容。@Michael我看不到添加内容,它会持续一段时间。呵呵,这太尴尬了。通过您的编辑,我得到了相同的结果。我想一定是某个地方掉了个轮子!我将不得不调查。我没有立即的答案,有关于马克的消息吗?
//------------------------------------------------------------------------------
// <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); }
  }

}