如何在C#项目中使用dll方式? [英] how mutch ways are there to use dll in C# projects?

查看:115
本文介绍了如何在C#项目中使用dll方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何在C#项目中使用dll的方式?



1-我们定义dll及其功能:

Hi,
how mutch ways are there to use dll in C# projects?

1- we define dll and its function with :

[DllImport("dllName.dll")]
public static extern void FunctionDll();





2-我们可以将dll文件作为参考(AddReference)添加到项目中,然后直接调用dll函数。



其他?



我们可以添加什么样的dll文件作为参考并调用它们的函数?



i有一个dll由VC ++(MFC)创建

C#项目中哪个最佳使用的方式是什么?



感谢您的帮助,



2- we can add dll file as reference (AddReference) to project then call dll functions, directly .

other ?

what kind of dll files can we add as reference and call their functions?

i have a dll that is created by VC++ (MFC).
Which way is the best using of that dll in C# project?

thank your help,

推荐答案

DLL文件有两种类型:Native和.NET,它们必须以不同的方式处理。

它们没有任何外部差异,就操作系统而言,它们都是扩展名为.DLL的文件。



原生DLL不包含.NET代码,a nd不能作为引用添加 - 您必须使用DllImport来访问文件中的方法。



.NET DLL包含程序集,并通过添加引用,(和使用行,如果有必要)然后可以被视为类在您的项目中 - 您可以创建实例,调用方法静态和实例,使用属性等



只能添加.NET DLL和EXE文件作为参考。

MFC C ++ DLL文件是本机DLL并且只能通过DllImport访问。
There are two types of DLL file: Native and .NET, and they have to be handled differently.
They do not have any "external" difference, they are both files with a ".DLL" extension as far as the OS is concerned.

Native DLLs do not contain .NET code, and cannot be added as a reference - you have to use DllImport to access the methods in the file.

.NET DLLs do contain assemblies, and are used by adding a reference, (and a using line if necessary) and can then be treated as if the classes were in your project - you can create instances, call methods both static and instance, use properties, etc.

Only .NET DLL and EXE files can be added as references.
An MFC C++ DLL file is a Native DLL and can only be accessed via DllImport.


未提及的是您还可以在运行时动态加载程序集。

来自SO的示例:这里 [ ^ ]



至于你应该使用哪一个,这取决于你的项目。
What wasn't mentioned is that you can also dynamically load assemblies at a runtime.
Example from SO:here[^]

As to which one you should use, it depends on your project.


这篇关于如何在C#项目中使用dll方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆