开发者

Can I control the visibility of the generated class with Protobuf-net?

开发者 https://www.devze.com 2023-02-12 22:38 出处:网络
I\'m using protobuf-net (thanks Marc :) ) to serialize my object model, but I want the object model not to be visible outside my assembly (specifically, I want it to be internal).

I'm using protobuf-net (thanks Marc :) ) to serialize my object model, but I want the object model not to be visible outside my assembly (specifically, I want it to be internal).

By default protobuf-net seems to genera开发者_运维问答te public partial classes. Can I tell it to mark the class internal?

Here's a cut down version of my .proto file:

package MyProject.Core.Persistence;

option optimize_for = SPEED;

message DataObject {
  required string name = 1;
  required int32 id = 2;
}

Which generates the following class definition:

[global::System.Serializable,
   global::ProtoBuf.ProtoContract(Name=@"DataObject")]
public partial class DataObject : global::ProtoBuf.IExtensible
{
  public DataObject () {}

  ...
}


Not at the moment; however you can edit csharp.xslt to suit your own needs.

This doesn't seem a common case, but it could be added as an option easily enough.


Solution:

you can add the following two lines before your message definition.

import "protobuf-net/protogen.proto";
option (.protobuf_net.fileopt).access = INTERNAL;

Full Sample Code:

package MyProject.Core.Persistence;

import "protobuf-net/protogen.proto";
option (.protobuf_net.fileopt).access = INTERNAL;

option optimize_for = SPEED;

message DataObject {
  required string name = 1;
  required int32 id = 2;
}

Reference:

The definition of message access in mgravell's protogen.proto

enum Access {
    option (.protobuf_net.enumopt) = {
       access: PUBLIC
    };
    INHERIT = 0;
    PUBLIC = 1;
    PRIVATE = 2;
    INTERNAL = 3;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号