一.创建动态链接库dll文件
新建 -> 项目->类库
名称为:dlltest11
添加函数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dlltest11
{
public class Class1
{
public string ShowMessage(string text)
{
if (text == "123")
{
return "PASS";
}
else
{
return "NG";
}
}
}
}
生成解决方案:名称为dlltest11.dll的文件
二:引用动态链接库dll文件
新建项目,以控制台应用程序为例
添加引用:浏览,添加创建好的dlltest11.dll文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Reflection;
using dlltest11;
namespace DLLtest
{
class Program
{
//引入动态链接库
[DllImport("dlltest.dll")]
public static extern void ShowMessage(string text);//动态链接库中方法
static void Main(string[] args)
{
Console.WriteLine("请输入数字:\n");
string content = Console.ReadLine();
//实例化
dlltest11.Class1 i = new dlltest11.Class1();
//调用动态链接库的方法
string ReturnValue = i.ShowMessage(content);
Console.WriteLine("{0}", ReturnValue);
}
}
}