2016-09-29 34 views
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); } 
    } 

} 

回答

0

這可能是正常的 - 它是爲生成的Java。

This

寫描述信息方面的數據的.proto文件。 運行protoc生成C#(如果您願意,可以使用Java/C++)。 在您的應用程序中,使用與消息類型關聯的構建器來創建消息的實例。 將數據序列化爲流。 在應用程序(或其他應用程序)的其他某個位置反序列化數據。 這個想法是,建設者是可變的,而他們建立的消息是不可變的。您可以將構建器與使用Set *方法的構建器一起使用,這些方法可以再次返回相同的構建器,也可以使用可用於對象初始化器的屬性。

+0

謝謝異步,但我應該注意到我在'proto2''protobuf-net'而不是'proto3',所以我使用'protogen.exe'而不是'protoc.exe'。無論如何,一個沒有完整構造函數的構建器需要init的setter屬性。 – BaltoStar

+0

「沒有完整構造函數的構建器需要init的setter屬性」並不是真的。 [見反射](http://stackoverflow.com/questions/934930/can-i-change-a-private-readonly-field-in-c-sharp-using-reflection)。另外,請注意你的類MyProto是「partial」。那麼在別處是否有setter或其他構造函數? – asynchronos

相關問題