Protobuf C# generates internal (versus public) constructor

541 Views Asked by At

For some reason the generated code contains internal constructor instead of public constructor:

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    internal MyProtobufMessage()
    {
        OnConstruction();
    }

From the official document, it is supposed to be public:

  // Parameterless constructor which calls the OnConstruction partial method if provided.
  public Foo();

I am wondering if there's any option that I can specify to generate a public constructor. I need it to be public because I am using generic template which requires the type to have a public constructor (new() constraint).

Protobuf version I am using: 3.11.4

The .proto file is something like this:

syntax = "proto3";
import "timestamp.proto";

package MyNamespace;
option csharp_namespace = "MyNamespace";

message MyProtobufMessage{
   ...
}
1

There are 1 best solutions below

0
On

There's an option to make the whole message classes internal, but AFAIK there should always be a public parameterless constructor.

Do you know exactly how your .protos file is being compiled?

N.B. that the code generation is done by a separate package Google.Protobuf.tools - you should check this package is also at 3.11.4 (and that your .proto compile is picking the latest version of protoc).

The compilation process should be a command similar to: C:\Users\Me\.nuget\packages\Google.Protobuf.tools\3.11.4\tools\windows_x64\protoc.exe .\test.proto --csharp_out .\