0
在什麼情況下protogen.exe
應用於.proto
文件生成C#
類,其中每個屬性只有一個getter(而不是setter)?protobuf-net:爲什麼生成的類會丟失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([email protected]"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, [email protected]"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); }
}
}
謝謝異步,但我應該注意到我在'proto2''protobuf-net'而不是'proto3',所以我使用'protogen.exe'而不是'protoc.exe'。無論如何,一個沒有完整構造函數的構建器需要init的setter屬性。 – BaltoStar
「沒有完整構造函數的構建器需要init的setter屬性」並不是真的。 [見反射](http://stackoverflow.com/questions/934930/can-i-change-a-private-readonly-field-in-c-sharp-using-reflection)。另外,請注意你的類MyProto是「partial」。那麼在別處是否有setter或其他構造函數? – asynchronos