类库工程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestDLL
{
public class Class1
{
public void Test()
{
Console.WriteLine("Test");
}
}
}
调用DLL的.Net Framework 控制台工程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
internal class Program
{
static void Main(string[] args)
{
// 加载 TestDLL.dll 程序集
Assembly assembly = Assembly.LoadFrom("dll/TestDLL.dll");
// 获取 TestDLL.Class1 类型
Type type = assembly.GetType("TestDLL.Class1");
// 创建 TestDLL.Class1 的实例
object obj = Activator.CreateInstance(type);
// 获取 Test 方法的 MethodInfo
MethodInfo method = type.GetMethod("Test");
// 调用 Test 方法
method.Invoke(obj, null);
// 等待用户按下任意键退出程序
Console.ReadKey();
}
}
}