c# - .NET 包装器到 C++ 代码 : CS0122 is inaccessible due to its protection level error

标签 c# c++ .net c++-cli wrapper

我正在尝试构建一个 C++/CLR 包装器以从 C# .Net 应用程序内部调用我的 C++ 代码。

步骤如下:

C++ 项目:

cppproject.h

#ifndef _CPPPROJECT_H
#define _CPPPROJECT_H


typedef enum {
    SUCCESS,
    ERROR
} StatusEnum;

namespace cppproject
{
    class MyClass {

    public:
        MyClass();
        virtual ~MyClass();
        StatusEnum Test(); 

    };
}

#endif 

cppproject.cpp

#include "cppproject.h"

namespace cppproject {

    MyClass::MyClass() {};
    MyClass::~MyClass() {};

    StatusEnum MyClass::Test() 
    { 
        return SUCCESS;
    }

} 

现在是将 C# 和 C++ 结合在一起的包装器项目(C++/CLR 类型):

wrapper.h

// wrapper.h

#pragma once

#include "cppproject.h"

using namespace System;

namespace wrapper {

    public ref class Wrapper
    {
        public:
            /*
             * The wrapper class
             */
            cppproject::MyClass* wrapper;

            Wrapper();
            ~Wrapper();

            StatusEnum Test();
    };
}

wrapper.cpp

// This is the main DLL file.

#include "stdafx.h"

#include "wrapper.h"

namespace wrapper {

    Wrapper::Wrapper()
    {
        wrapper = new cppproject::MyClass();
    }

    Wrapper::~Wrapper()
    {
        delete wrapper;
    }

    StatusEnum Wrapper::Test() 
    { 
        return wrapper->Test(); 
    };
}

最后是 C# 代码,我在其中遇到错误:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using wrapper;


namespace netproject
{
    /*
     * Enums
     */
    public enum StatusEnum {
        SUCCESS,
        ERROR
    };

    public partial class netproject
    {
        public const int MAX_REPORT_DATA_SIZE = 1024;
        public wrapper.Wrapper wrapper;

        public netproject() { wrapper = new wrapper.Wrapper(); }
        ~netproject() { wrapper = null; }

        public StatusEnum Test() 
        { 
            var sts = wrapper.Test(); <<- ERROR
            return (netproject.StatusEnum) sts;<<- ERROR 
        }
    }
}

C#项目的编译错误:

error CS0122: 'wrapper.Wrapper.Test()' is inaccessible due to its protection level
error CS0426: The type name 'StatusEnum' does not exist in the type 'netproject.netproject'

我无法理解。 Test 在包装器项目和 C++ 项目中都被定义为 public。并且 StatusEnum 在错误行上方的 C# 项目中也是公共(public)的。

感谢帮助了解这里发生了什么....

最佳答案

typedef enum {
    SUCCESS,
    ERROR
} StatusEnum;

这不是在 C# 中可以访问的东西。在我看来,您有两个选择:

1) 您可以使枚​​举成为托管枚举。

public enum class StatusEnum {
    SUCCESS,
    ERROR
};

2) 我通常不喜欢只有两个值的枚举。在许多情况下, bool 值也同样有效。

public ref class Wrapper
{
    // returns true on success.
    bool Test();
};

关于c# - .NET 包装器到 C++ 代码 : CS0122 is inaccessible due to its protection level error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39455154/

相关文章:

c# - 扩展方法的有效权重(在设计时、运行时等)

c# - 使用 C# Lambda 运算符

.net - 在 IIS 7.0 上访问 .asmx 页面时出现 "Could not create type XXXX"

c# - ListView 中的 WPF 按钮在 ViewModel 中看不到命令

c++ - OpenGL不报告着色器编译器错误

C++ 重写 >> 运算符

c++ - 检查c++中是否存在函数时出现问题

c# - ClosedXML 是否支持设置工作表的缩放级别?

c# - SandcaSTLe 如何知道 vs-project 成员的可见性

c# - 安装有效但无法运行该应用程序