为给定的 .proto 文件生成 C# 代码会在打开文件时产生错误

Generating C# code for a given .proto file produces an error while opening a file

我正在研究 Cpp 编写的 dll,它将在我的 C# 项目中使用。我使用 google::protobuf::compiler::csharp::Generator 生成 .cs 文件。

首先,我创建 google::protobuf::compiler::Importer。为此,我需要获取 DiskSourceTree 的实例并实施 MultiFileErrorCollector:

class ErrorCollector : public MultiFileErrorCollector
{
public:
    void AddError(const std::string& filename, int line, int column, const std::string& message) override
    {
        std::fstream stream;
        stream.open(filename);
        stream << message;
        stream.close();
    }
    void AddWarning(const std::string& filename, int line, int column, const std::string& message) override
    {
        std::fstream stream;
        stream.open(filename);
        stream << message;
        stream.close();
    }
};

之后,我实现 GeneratorContext 将其传递给 Generator::Generate():

class Context : public GeneratorContext
{
public:
    google::protobuf::io::ZeroCopyOutputStream* Open(const std::string& filename)
    {
        stream_ptr = std::make_unique<std::fstream>();
        stream_ptr->open(filename);
        proto_stream = std::make_unique<google::protobuf::io::OstreamOutputStream>(stream_ptr.get());
        return proto_stream.get();
    }

private:
    std::unique_ptr<std::fstream> stream_ptr;
    std::unique_ptr<google::protobuf::io::OstreamOutputStream> proto_stream;
};

错误发生在导入.proto-文件的阶段。调试器说,const google::protobuf::FileDescriptor* desc = importer->Import(FILENAME); 结果为 null

这可能与文件路径有关,甚至与我对它的工作原理的理解有关。如果有任何帮助,我将不胜感激。

这是我的 main 函数:

int main()
{
        // building an Importer
        DiskSourceTree* tree = new DiskSourceTree;
        ErrorCollector* collector = new ErrorCollector;

        Importer* importer = new Importer(tree, collector);
    
        const std::string FILENAME = "C:/my/path/my_file.proto";
        const google::protobuf::FileDescriptor* desc = importer->Import(FILENAME); // the error is here
    
        // generating the code
        Generator generator;
        Context* context = new Context();
        std::string* error_str = new std::string;
        error_str->reserve(256);
    
        if (generator.Generate(desc, "", context, error_str)) // this line produces an exception since the descriptor is invalid
        {
            std::cout << "success!";
        }
    
        delete tree;
        delete collector;
        delete importer;
        delete context;
        delete error_str;
    }

事实证明,这些是用于特定自定义的一些额外代码。

如果您所做的只是生成一个 proto-class(以任何支持的语言),您只需在 google::protobuf::compiler::CommandLineInterface:

中注册生成器
#include <google/protobuf/compiler/command_line_interface.h>
#include <google/protobuf/compiler/csharp/csharp_generator.h>

using namespace google::protobuf::compiler;    

int main(int argc, char** argv)
{
    CommandLineInterface cli;    
    csharp::Generator generator;

    cli.RegisterGenerator("--cs_out", &generator, "Generate C# source.");
    return cli.Run(argc, argv);
 }

Command line arguments and some other details