0

Under what circumstances could protogen.exe applied to .proto file generate C# classes where each property has only a getter ( not 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); }
  }

}
BaltoStar
  • 8,165
  • 17
  • 59
  • 91

1 Answers1

0

That may be normal -- it is for generated Java.

This says

Write a .proto file describing your data in terms of messages. Run protoc to generate C# (and Java/C++ if you so wish). In your application, use the builder associated with the message type to create an instance of a message. Serialize the data to a stream. At some other point in the application (or a different app) deserialize the data. The idea is that builders are mutable, while the messages they build are immutable. You can use builders either with Set* methods which return the same builder again, or properties which can be used within object initializers.

asynchronos
  • 583
  • 2
  • 14
  • thanks asynchronous but I should have notated that I'm on `proto2` `protobuf-net` not `proto3` so I'm using `protogen.exe` not `protoc.exe`. At any rate, a builder in the absence of a full constructor requires properties with setters for init. – BaltoStar Sep 30 '16 at 17:51
  • It's not true that "a builder in the absence of a full constructor requires properties with setters for init." [See reflection](http://stackoverflow.com/questions/934930/can-i-change-a-private-readonly-field-in-c-sharp-using-reflection). Also, note your class MyProto is `partial`. So are there setters or another constructor elsewhere? – asynchronos Oct 03 '16 at 17:34