C#封装dll及使用
- 首先创建一个类库
举个简单的例子:
namespace mydll
{
public class Class1
{
public int add(int a,int b)
{
int c = a + b;
return c;
}
}
}
并点击生成解决方案,随后会自动在其Debug目录下生成dll文件。
-
再新建一个项目来使用dll
我这里是新建个控制台应用程序 -
添加引用
在解决方案资源管理器—引用—添加引用
点击浏览选择要添加的dll文件
添加成功后可以在引用这里查看到
-
使用封装的dll
使用前要先声明(既 using Mydll;)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mydll;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Class1 dll = new Class1();
int D=dll.add(4,5);
Console.Write(D);
Console.ReadKey();
}
}
}
输出结果:9