繁体   English   中英

为 given.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 function:

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;
    }

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

如果您所做的只是生成一个原型类(使用任何支持的语言),您只需在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);
 }

命令行 arguments 和一些其他细节

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM