How to generate struct with ProtoGen from proto file

試著忘記壹切 提交于 2019-12-07 14:20:14

问题


We've been using protobuf-net ProtoGen to generate C# .cs file from *.proto file. We would like instead of class to generate struct. e.g.

[DataContract]
public struct Entity1
{
    [ProtoMember(1)]
    public double Field1 { get; set; }

    [ProtoMember(2)]
    public string Field2 { get; set; }
}

I tried to extract protocol buffer using method Serializer.GetProto for class and struct type. It seems, that protocol buffer (.proto) doesn't recognize difference between the struct and class. So I imply, that could be a parameter in ProtoGen to choose between class/struct for generation from .proto. Does the feature exist or how could I solve generation of the struct from *.proto?


回答1:


At the current time, ProtoGen only emits classes; since the data is inherently mutable, I would say that this is reasonable, but - two options present themselves:

  • edit csharp.xslt to replace class with struct (or copy csharp.xslt, and use the -t parameter to tell it to use the copy)
  • use it "as is", and make judicious use of ctrl+h (low-tech, I know; but it will work)


来源:https://stackoverflow.com/questions/18358233/how-to-generate-struct-with-protogen-from-proto-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!