[英]Can protobuf-net output .proto files that will be used for code generation in C++?
我試圖弄清楚protobuf-net
是否能夠生成.proto
文件,然后可以將這些文件與常規 Protobuf 編譯器一起使用來編譯 C++ 類。
我看了那些問題
但他們真的很老,談論 V2 Protobuff 語法。
我的目標是在 C# 中編寫類層次結構,然后使用protobuf-net
為所述層次結構生成.proto
文件。 然后我想使用官方的 Google Complier 來生成基於該層次結構的 C++ 代碼。
最終結果應該在 C# 和 C++ 之間具有字段和數據奇偶校驗,以便可以交換消息。
這可能嗎?
我將嘗試提供一個示例。
假設我在 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);
}
}
我想使用protobuf-net
生成一個.proto
文件以在 C++ 中生成相同的層次結構。
我的問題是: protobuf-net
是否可行? 如果您認為缺少更多信息,請告訴我缺少什么,我會提供。
是和不是。 Protobuf-net 同時支持 proto2 和 proto3,所以:這很好 - 它通常在 GetSchema/GetProto 中做得很好,可以正常工作。 但是,我看到您的特定模型的問題是:繼承。 Protobuf 並沒有真正做繼承。 Protobuf-net 可以以與oneof
in.proto 條款兼容的方式偽造它,但是:它並不理想。 如果您想要一個能夠很好地映射到跨平台使用的模型,您主要需要從 .proto 支持的簡單模型開始,其中不包括它。
但是:注釋模型並試一試!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.