问题
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.xsltto replaceclasswithstruct(or copycsharp.xslt, and use the-tparameter 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