Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于C++/CLI和C#集成_C#_C++ Cli - Fatal编程技术网

关于C++/CLI和C#集成

关于C++/CLI和C#集成,c#,c++-cli,C#,C++ Cli,晚安 我试图在C++/CLI中创建一个简单的dll,以便在我的C#library中使用,代码如下: // This is the main DLL file. #include "stdafx.h" namespace Something { public class Tools { public : int Test (...) { (...) } } } 我可以编译dll并将其毫无问题地加载

晚安

我试图在C++/CLI中创建一个简单的dll,以便在我的C#library中使用,代码如下:

// This is the main DLL file.

#include "stdafx.h"

namespace Something
{
    public class Tools
    {
        public : int Test (...)
        {
            (...)
        }
    }
}
我可以编译dll并将其毫无问题地加载到C#项目中,还可以使用C#中的名称空间和类工具。问题是,当我尝试编写Tools.Test(某物)时,我会收到一条错误消息,说Tools没有测试的定义。为什么编译器不能获取函数,即使它被声明为公共的

还有。。。我可以在两个项目中共享一个类吗?一半是用C写的,一半是用C++编写的?
非常感谢。

功能不是静态的。试试这个

var someTools = new Tools();
int result = someTools.Test(...);
或者将方法设置为静态:

public : 
   static int Test (...)
   {
            (...)
   }

C只能访问托管C++类。您需要使用

public ref class Tools
来指示Tools是一个托管类,以便可以从C#访问它。有关更多信息,请参阅


此类可以在托管C++或C语言中使用。注意,托管C++类也可以在内部使用本机C++类。

可以跨项目共享托管类,但可以在非托管(即标准C++类)中编写。使用C++代码> REF类< /Cord>关键字定义C++中托管类。
// This is the main DLL file.

#include "stdafx.h"

namespace Something
{
    public ref class Tools
    {
        public : int Test (...)
        {
            (...)
        }
    }
}

向我们展示代码的另一半,即尝试调用测试但失败的部分。Preet,Miguel的示例不起作用的原因不是因为该方法不是静态的,而是因为该类不是ref类,因此对CLR不可见。将该方法设为静态不会改变这一点。