从 DLL 中调用函数?
我是 C# 新手,我正在尝试学习 DLL 的使用。我试图将我的对象包装在 DLL 中,然后在我的程序中使用它。
public class Foo // its in the DLL
{
public void Bar()
{
SomeMethodInMyProgram();
}
}
所以我尝试将其打包到 DLL 中,但我不能,因为编译器不知道 SomeMethodInMyProgram() 是什么。
我想这样使用它:
class Program // my program, using DLL
{
static void Main(string[] args)
{
Foo test = new Foo();
test.Bar();
}
}
I'm new to C# and I'm trying to learn to usage of DLLs. I'm trying to wrap my objects in a DLL, and then use it in my program.
public class Foo // its in the DLL
{
public void Bar()
{
SomeMethodInMyProgram();
}
}
So I try to pack this to a DLL but I can't, because compiler doesn't know what the SomeMethodInMyProgram() is.
I would like to use it like:
class Program // my program, using DLL
{
static void Main(string[] args)
{
Foo test = new Foo();
test.Bar();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
取决于什么类型的 DLL。这是内置于 .NET 中的吗?如果它是非托管代码,那么这里是一个示例,否则 Rob 的答案将起作用。
非托管 C++ dll 示例:
您可能需要使用 DllImport
或
然后每个都按如下方式调用:
Depends on what type of DLL. Is this built in .NET ? if it is unmanaged code then here is an example otherwise the Answer from Rob will work.
Unmanaged C++ dll example:
You may need to use DllImport
or
Then each of those are called like this:
通过解决方案资源管理器添加 DLL - 右键单击引用 -->添加引用,然后“浏览”到您的 DLL - 那么它应该可用。
Add the DLL via the solution explorer - right click on references --> add reference then "Browse" to your DLL - then it should be available.
我参加这里的聚会迟到了,但我把这个答案留给像我这样拔头发的人。所以基本上,在面对这个问题时,我没有 VS IDE 的奢侈。我试图使用 csc 通过 cmdline 编译代码。为了引用 dll,只需将编译器标志 /r:PathToDll/NameOfTheDll 添加到 csc 即可。
该命令看起来像
在 FileWhichIsReferencingTheDll.cs 中添加
using namespace AppropriateNameSpace;
来访问函数(如果是静态或静态,则通过调用 class.functionName通过创建该类的对象并调用该对象上的函数)。I am late to the party here but am leaving this answer for someone pulling his/her hair out like me. So basically, I did not have the luxury of VS IDE when facing this issue.I was trying to compile the code via cmdline using csc. In order to reference a dll, just add the compiler flag /r:PathToDll/NameOfTheDll to csc.
The command would look like
In FileWhichIsReferencingTheDll.cs add
using namespace AppropriateNameSpace;
to access the functions (by calling class.functionName if static or by creating an object of the class and invoking the function on the object).您需要在运行时实际将 DLL 加载到应用程序中,即 DLL 的动态部分。您还需要定义 DLL 中的函数的头文件,以便您的编译知道已定义了哪些函数。我这里的知识是基于 C++ 的,所以这对于 C# 是如何工作的我不确定,但它会是这样的......
you need to actually load the DLL into your application at run time, thus the Dynamic part of DLL. You also need the header file that defines what functions are in the DLL so your compile knows what functions have been defined. My knowledge here is based on C++ so how this works for C# I am not to sure, but it will be something like that...
这是我的 DLL (AllInOne) 源代码,其中有一个名为Calculate 的类,该类具有 GetAreaofSquare 方法。
我已在项目的解决方案资源管理器(它是一个控制台应用程序)中的引用中添加了此 DLL,并在系统命名空间中添加了 AllInOne。请仔细阅读“使用AllInOne”。我们可以实例化如下所示的Calculate类,然后可以使用GetAreaofSquare方法来计算Square的面积。
This is my source code for DLL (AllInOne) having a class named Calculate which has a method GetAreaofSquare.
I have added this DLL in the reference located in the solution explorer of the project, which is a console application, and added AllInOne in the system namespace. Please see carefully "using AllInOne". We can instantiate Calculate class as shown below and then can use the method GetAreaofSquare to calculate the area of the Square.