创建自己的链接库步骤:
1: 在 “新建项目”中找到 “类库” ,选择好项目路径,然后确认,就创建好了解决方案。
2: 编写自己的测试函数比如---
public void ShowMessage()
{
Console.WriteLine("你已经成功调用了动态链接!");
Console.ReadLine();
}
3: 在解决方案资源管理器中 找到你的项目根目录然后右键打开 “属性” 设置你的dll文件名等。
4: 在 横向菜单栏上找到 “生成” 找到 “生成 xxx” ,默认的生成路径在 bin/debug 下。
使用dll:
1 新建一个 “控制台程序”
2 引入 System.Runtime.InteropServices 命名空间,在 "解决方案资源管理器"中找到目录中的 “引用” 右键 “添加引用” ,找到自己刚才dll项目路径的dll文件,点击确认完成添加。
3 代码中引入 你的 dll 中想用到的命名空间
4 使用你想用到类的 public 成员函数
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using ClassLibrary1;
namespace DLLExample
{
class Program
{
//[DllImport("DLLTest.dll")]
//public static extern void ShowMessage();
static void Main(string[] args)
{
Class1 i = new Class1();
i.ShowMessage();
}
}
}