[英]Can protobuf-net output .proto files that will be used for code generation in C++?
I'm trying to figure out if protobuf-net
is capable of generating .proto
files that then can be used with the regular Protobuf compiler to compile C++ classes.我试图弄清楚
protobuf-net
是否能够生成.proto
文件,然后可以将这些文件与常规 Protobuf 编译器一起使用来编译 C++ 类。
I looked at those questions我看了那些问题
Protobuf-net.proto file generation for inheritance Protobuf-net.proto文件生成用于继承
protobuf-net inheritance shows base class in.proto file protobuf-net继承显示.proto文件中的基类
Protobuf-net is incompatible with official google Protobuf for C++ (message encoding) Protobuf-net 与 C++ 的官方谷歌 Protobuf 不兼容(消息编码)
But they are really old and talk about V2 Protobuff syntax.但他们真的很老,谈论 V2 Protobuff 语法。
My goal is to write a class hierarchy in C# and then use protobuf-net
to generate .proto
file for said hierarchy.我的目标是在 C# 中编写类层次结构,然后使用
protobuf-net
为所述层次结构生成.proto
文件。 Then I want to use the official Google Complier to generate C++ code based on that hierarchy.然后我想使用官方的 Google Complier 来生成基于该层次结构的 C++ 代码。
The end result should have field and data parity between C# and C++ so that messages could be exchanged.最终结果应该在 C# 和 C++ 之间具有字段和数据奇偶校验,以便可以交换消息。
Is this possible?这可能吗?
I'll try to provide an example.我将尝试提供一个示例。
Suppose I have the following code in C#:(Might have some syntactic mistakes as I typed it out quickly in notepad)假设我在 C# 中有以下代码:(可能有一些语法错误,因为我在记事本中快速输入了它)
public class Point{
public double x;
public double y;
}
public abstract class Polygon{
public List<Point> Vertices;
public Polygon(){
Vertecies = new();
}
public override ToString(){
string ans = "";
foreach(var point in Vertecies){
ans += $"({point.x},{point.y})"
}
return ans;
}
}
public class Square: Polygon{
public Sqare(Point a, Point b, Point c Point d){
this.Vertices.Add(a);
this.Vertices.Add(b);
this.Vertices.Add(c);
this.Vertices.Add(d);
}
}
public class Triangle: Polygon{
public Triangle(Point a, Point b, Point c){
this.Vertices.Add(a);
this.Vertices.Add(b);
this.Vertices.Add(c);
}
}
I want to use protobuf-net
to generate a .proto
file to generate this same hierarchy in C++.我想使用
protobuf-net
生成一个.proto
文件以在 C++ 中生成相同的层次结构。
My question is: If this is possible with protobuf-net
?我的问题是:
protobuf-net
是否可行? If you think further information is missing, please tell me what is missing and I'll provide it.如果您认为缺少更多信息,请告诉我缺少什么,我会提供。
Yes and no.是和不是。 Protobuf-net supports both proto2 and proto3, so: that's fine - and it usually does a good enough job in GetSchema/GetProto to work fine.
Protobuf-net 同时支持 proto2 和 proto3,所以:这很好 - 它通常在 GetSchema/GetProto 中做得很好,可以正常工作。 The problem I see if your specific model, however, is: inheritance.
但是,我看到您的特定模型的问题是:继承。 Protobuf doesn't really do inheritance.
Protobuf 并没有真正做继承。 Protobuf-net can fake it, in a way that is compatible with
oneof
in.proto terms, but: it isn't ideal . Protobuf-net 可以以与
oneof
in.proto 条款兼容的方式伪造它,但是:它并不理想。 It you want a model that maps well to cross-platform usage, you mostly need to start with the simple model that is supported by.proto, which doesn't include that.如果您想要一个能够很好地映射到跨平台使用的模型,您主要需要从 .proto 支持的简单模型开始,其中不包括它。
But: annotate the model and give it a whirl!但是:注释模型并试一试!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.