![](/img/trans.png)
[英]protobuf-net - generated class from .proto - Is repeated field supposed to be Read Only with no setter?
[英]protobuf-net : why would generated class be missing setter?
在什么情況下,應用於.proto
文件的protogen.exe
可以生成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); }
}
}
這可能是正常的-這是針對生成的Java。
這說
編寫一個.proto文件,以消息的形式描述您的數據。 運行protoc生成C#(如果願意,還可以生成Java / C ++)。 在您的應用程序中,使用與消息類型關聯的構建器來創建消息的實例。 將數據序列化為流。 在應用程序(或其他應用程序)的其他地方,對數據進行反序列化。 這個想法是,構建器是可變的,而他們構建的消息是不可變的 。 您可以將構建器與Set *方法一起使用,以再次返回相同的構建器,也可以在對象初始化器中使用屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.