关于 C++/CLI 和 C# 集成的一些问题
晚安,
我试图在 C++/CLI 中制作一个简单的 dll,以便在我的 c# 库中使用,使用类似以下代码的代码:
// This is the main DLL file.
#include "stdafx.h"
namespace Something
{
public class Tools
{
public : int Test (...)
{
(...)
}
}
}
我可以编译 dll 并将其加载到 C# 项目中,没有任何问题,并且可以使用命名空间 Something以及 C# 中的工具类。问题是,当我尝试编写 Tools.Test(something) 时,我收到一条错误消息,指出 Tools 没有 Test 的定义。为什么即使声明为 public,编译器也无法获取该函数?
另外...我可以在两个项目之间共享一个类,一半用 C# 编写,一半用托管 C++ 编写吗?
非常感谢。
Good night,
I was trying to make a simple dll in C++/CLI to use in my c# library using something like the following code:
// This is the main DLL file.
#include "stdafx.h"
namespace Something
{
public class Tools
{
public : int Test (...)
{
(...)
}
}
}
I can compile the dll and load it into the C# project without any problems, and can use the namespace Something and the class Tools from C#. The problem is that when I try to write Tools.Test(something) I get an error message saying that Tools doesn't have a definition for Test. Why can't the compiler get the function, even if it is declared public?
Also... Can I share a class across two project, half written in C# and half written in managed C++?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C# 只能访问托管 C++ 类。您需要使用
public ref class Tools
来指示 Tools 是托管类,以便可以从 C# 访问它。有关详细信息,请参阅 msdn。然后,此类可以在托管 C++ 或 C# 中使用。请注意,托管 C++ 类也可以在内部使用本机 C++ 类。
C# can only access managed C++ classes. You would need to use
public ref class Tools
to indicate that Tools is a managed class to make it accessible from C#. For more info see msdn.This class can then be used in either managed C++ or C#. Note that managed C++ classes can also use native C++ classes internally.
您可以在项目中共享托管类,但可以在非托管(即标准 C++ 类)中共享托管类。使用
ref class
关键字可以在 C++ 中定义托管类。You can share a managed class across a project, but what you've written in an unmanaged (i.e. standard C++ class. Use the
ref class
keyword to define a managed class in C++.该函数不是静态的。在 中尝试此操作
或使方法静态:
The function is not static. try this in the
or make the method static :