【问题标题】:protobuf-net : why would generated class be missing setter?protobuf-net:为什么生成的类会缺少 setter?
【发布时间】:2016-09-29 20:43:27
【问题描述】:

在什么情况下protogen.exe 可以应用于.proto 文件生成C# 类,其中每个属性只有一个getter(而不是setter)?

package MyLibrary.MyProto                                                               

import "MyExternalType.proto";                                                                                                                          
option optimize_for = SPEED;                                                                        

message MyProto {                                                                           
    repeated MyExternalType MyProperty = 1;                                                 
}


//------------------------------------------------------------------------------
// <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: MyLibrary.MyProto
// Note: requires additional types generated from: MyExternalType.proto
namespace MyLibrary
{
  [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"MyProto")]
  public partial class MyProto : global::ProtoBuf.IExtensible
  {
    public MyProto() {}

    private readonly global::System.Collections.Generic.List<MyExternalType> _MyProperty = new global::System.Collections.Generic.List<MyExternalType>();
    [global::ProtoBuf.ProtoMember(1, Name=@"MyProperty", DataFormat = global::ProtoBuf.DataFormat.Default)]
    public global::System.Collections.Generic.List<MyExternalType> MyProperty
    {
      get { return _MyProperty; }
    }

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

}

【问题讨论】:

    标签: c# code-generation protocol-buffers protobuf-net protogen


    【解决方案1】:

    这可能是正常的——它是用于生成的 Java。

    This

    编写一个 .proto 文件,以消息的形式描述您的数据。 运行 protoc 以生成 C#(如果您愿意,还可以生成 Java/C++)。 在您的应用程序中,使用与消息类型关联的构建器来创建消息的实例。 将数据序列化为流。 在应用程序(或不同的应用程序)中的某个其他点反序列化数据。 这个想法是构建器是可变的,而他们构建的消息是不可变的。您可以将构建器与再次返回相同构建器的 Set* 方法或可在对象初始化器中使用的属性一起使用。

    【讨论】:

    • 感谢异步,但我应该指出我在 proto2 protobuf-net 而不是 proto3 所以我使用的是 protogen.exe 而不是 protoc.exe。无论如何,在没有完整构造函数的情况下,构建器需要具有用于 init 的 setter 的属性。
    • “没有完整构造函数的构建器需要带有用于初始化的设置器的属性”是不正确的。 See reflection。另外,请注意您的 MyProto 课程是 partial。那么其他地方是否有 setter 或其他构造函数?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    相关资源
    最近更新 更多