0

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

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

}
4

1 回答 1

0

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

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

于 2016-09-30T16:31:23.713 回答