cognize

C#动态加载dll或exe类库(即C#反射)

头部引用代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;//一般前四行代码在我们创建程序时自动生成,我们只需要加入本行代码即可
```<!--more-->
主要代码如下:
```csharp
Assembly ass = Assembly.LoadFile(@"D:\mydll.dll");//我们要调用的dll或exe文件路径
Type tp = ass.GetType("namepace.class"); //获取类名,必须 命名空间+类名
Object obj = Activator.CreateInstance(tp); //建立实例
MethodInfo meth = tp.GetMethod("mymethod"); //获取要调用的方法
meth.Invoke(obj, new string[] {"args1","args2"});//Invoke调用方法,"{}"里为方法需<span style="font-family: Arial, Helvetica, sans-serif;">要的参数</span>

标签:

发表于2014-10-21 15:55:39,最后修改于2015-08-12 22:22:53。

本站文章欢迎链接分享,禁止全文转载。


« 上一篇 c++在未安装vs的电脑上运行(即解决“由于应用程序的配置不正确,应用程序未能启动”问题) 下一篇 » C#创建XML文件

推荐阅读

Big Image