简体   繁体   English

protobuf-net 可以输出将用于 C++ 代码生成的 proto 文件吗?

[英]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我看了那些问题

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.

相关问题 protobuf-net-反引号,字典和.proto文件 - protobuf-net - backticks, Dictionaries & .proto files 使用Google Protobuf proto2文件生成C#代码 - C# code generation with Google Protobuf proto2 files 是否可以使用 protobuf-net 为扩展名为“FieldOptions”的 proto2 文件生成 C# 文件? - Is it possible to generate C# files using protobuf-net for proto2 files with extension of 'FieldOptions'? 是否可以通过生成.proto文件,用python反序列化用C#中的protobuf-net序列化的对象? - Can an object serialized with protobuf-net in c# be deserialized with python by generating a .proto file? 使用protobuf-net生成C#时保留proto注释 - Preserving proto comments when generating C# with protobuf-net protobuf-net与C ++通信 - protobuf-net communicating with C++ 使用自定义十进制原型契约(C#/ C ++互操作)时,使用Protobuf-net(de)序列化小数 - Protobuf-net (de)serialization of decimals throws when using custom decimal proto contract (C#/C++ interop) 使用protobuf-net的C#项目中的协议缓冲区 - 代码生成的最佳实践 - Protocol buffers in C# projects using protobuf-net - best practices for code generation protobuf-net [默认]在.proto文件中无法识别? - protobuf-net [default] in .proto file not recognized? .proto文件中带有oneof的Protobuf-net - Protobuf-net with oneof in .proto file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM