利用C#的反射机制动态调用DLL类库
好,在这之前我先把反射所需要使用的几个类给大家列一下:
1、使用Assembly类定义和加载程序集,加载在程序集清单中列出模块,以及从此程序集中查找类型并创建该类型的实例。
2、使用MethodInfo了解方法的名称、返回类型、参数、访问修饰符(如pulic 或private)和实现详细信息(如abstract或virtual)等。使用Type的GetMethods或GetMethod方法来调用特定的方法。
一、创建用于反射调用的DLL
using System;
using System.Collections.Generic;
using System.Text;
namespace RefDll
{
/// <summary>
/// 创建需要被调用的DLL类库
/// </summary>
public class RefTest
{
/// <summary>
/// 求和方法
/// </summary>
/// <param name="x">第一个值</param>
/// <param name="y">第二个值</param>
/// <param name="sum">结果(和)</param>
public void TestSum(int x,int y,out int sum)
{
sum = ;
sum = x + y;
}
/// <summary>
/// 求和方法
/// 第二种方式
/// </summary>
/// <param name="x">第一个值</param>
/// <param name="y">第二个值</param>
/// <returns>结果(和)</returns>
public int TestSumTwo(int x, int y)
{
return x + y;
}
/// <summary>
/// 求和方法
/// 第三种方式
/// </summary>
/// <param name="x">第一个值</param>
/// <param name="y">第二个值</param>
/// <param name="sum">结果(和)</param>
public static void TestSumThree(int x, int y, out int sum)
{
sum = ;
sum = x + y;
}
}
}
二、应用于反射的例子
注:可以创建一个控制台的工程。
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
namespace ReflectionLesson
{
/// <summary>
/// 反射类
/// 利用反射动态调用DLL类库。
/// </summary>
public class ReflectionLesson
{
private string strDllName = "";
private string strClaName = "";
private string[] strMetName = null;
/// <summary>
/// 构造方法
/// </summary>
/// <param name="DllName">调用的DLL类库名</param>
/// <param name="ClaName">调用的类名</param>
/// <param name="MetName">调用的方法名(数组)</param>
public ReflectionLesson(string DllName, string ClaName, string[] MetName)
{
//获取调用的DLL类库
this.strClaName = ClaName;
this.strDllName = DllName;
this.strMetName = MetName;
}
/// <summary>
/// 利用反射动态调用DLL类库
/// </summary>
public void ReflectionTest(int x,int y)
{
Assembly ass;
Type type;
object obj;
if (File.Exists(Application.StartupPath + "\\" + this.strDllName + ".dll"))
{
//获取并加载DLL类库中的程序集
ass = Assembly.LoadFile(Application.StartupPath + "\\" + this.strDllName + ".dll");
//获取类的类型:必须使用名称空间+类名称
type = ass.GetType(this.strDllName + "." + this.strClaName);
//获取类的方法:方法名称
MethodInfo method1 = type.GetMethod(this.strMetName[]);
MethodInfo method2 = type.GetMethod(this.strMetName[]);
MethodInfo method3 = type.GetMethod(this.strMetName[]);
//对获取的类进行创建实例。//必须使用名称空间+类名称
obj = ass.CreateInstance(this.strDllName + "." + this.strClaName);
//开始搜索方法
method1 = type.GetMethod(this.strMetName[]);//方法的名称1
method2 = type.GetMethod(this.strMetName[]);//方法的名称2
method3 = type.GetMethod(this.strMetName[]);//方法的名称3
object[] parts = new object[];
parts[] = x;
parts[] = y;
//方法的调用
//注:如果调用的DLL类库中方法是静态的,那么Invoke方法中第一个参数传值为NULL。
// 如果方法不是静态的,那么Invoke方法中第一个参数传值为 obj(上面那个被实例的对象)
method1.Invoke(obj, parts);
Console.WriteLine("调用的方法 " + this.strMetName[] + ": " + x + " + " + y + " = " + parts[]);
int sum1 = (int)method2.Invoke(obj, new object[] { x + , y + });
Console.WriteLine("调用的方法 " + this.strMetName[] + ": " + (x + ) + " + " + (y + ) + " = " + sum1); object[] temParts = new object[];
temParts[] = x + ;
temParts[] = y + ;
method3.Invoke(null, temParts);
Console.WriteLine("调用的方法 " + this.strMetName[] + ": " + temParts[] + " + " + temParts[] + " = " + temParts[]);
}
}
}
}
在Main 函数中可以输入以下代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ReflectionLesson
{
class Program
{
static void Main(string[] args)
{
string dllname = "RefDll";
string claname ="RefTest";
string[] metname = new string[]{"TestSum","TestSumTwo","TestSumThree"};
ReflectionLesson refl = new ReflectionLesson(dllname, claname, metname);
refl.ReflectionTest(,);
}
}
}
好了。现在可以跑一下如何调用的了,大家可以设置在调试模式下进行阅读代码。
利用C#的反射机制动态调用DLL类库的更多相关文章
- C# 通过反射类动态调用DLL方法
网上看了很多关于反射的思路和方法,发现这个还算不错 //使用反射方: using System; using System.Collections.Generic; using System.Linq ...
- C#程序实现动态调用DLL的研究(转)
摘 要:在<csdn开发高手>2004年第03期中的<化功大法——将DLL嵌入EXE>一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在可执行文件运行时,自动从资 ...
- 用C#通过反射实现动态调用WebService 告别Web引用
我们都知道,调用WebService可以在工程中对WebService地址进行WEB引用,但是这确实很不方便.我想能够利用配置文件灵活调用WebService.如何实现呢? 用C#通过反射实现动态调用 ...
- C#程序实现动态调用DLL的研究[转]
摘 要: 在< csdn 开发高手> 2004 年第 03 期中的<化功大法——将 DLL 嵌入 EXE >一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在 ...
- C#程序实现动态调用DLL的研究
摘 要:在<csdn开发高手>2004年第03期中的<化功大法——将DLL嵌入EXE>一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在可执行文件运行时,自动从资 ...
- 用C#通过反射实现动态调用WebService 告别Web引用(转载)
我们都知道,调用WebService可以在工程中对WebService地址进行WEB引用,但是这确实很不方便.我想能够利用配置文件灵活调用WebService.如何实现呢? 用C#通过反射实现动态调用 ...
- Java反射机制动态代理
1.什么事反射机制动态代理 在一段代码的前后动态执行其他操作,比如有一个方法是往数据库添加一个记录,我们可以通过动态代理,在操作数据库方法的前和后添加代码执行打开数据库连接和关闭数据库连接. 2.演示 ...
- C++利用模板在Windows上快速调用DLL函数
更新日志 --------- 2021/08/01 更新V2.2 增加 GetHmodule 函数 - 允许用户获取HMODULE以验证加载DLL是否成功. 2021/08/03 更新V2.3 增加 ...
- 卸载AppDomain动态调用DLL异步线程执行失败
应用场景 动态调用DLL中的类,执行类的方法实现业务插件功能 使用Assembly 来实现 但是会出现逻辑线程数异常的问题 使用AppDomain 实现动态调用,并卸载. 发现问题某个插件中开启异步线 ...
随机推荐
- java多线程编程(2)交替输出数字和字母
mark一下,不停的看看notify和wait的没有理解 class Printer { int index=0; //输出奇数 public synchronized void printA(int ...
- linux下swftools 的配置
1.安装所需的库和组件.机器之前安装过了,主要安装的是下面几个组件.如果不安装会提示machine `x86_64-unknown-linux' not recognized yum install ...
- selenium.common.exceptions.TimeoutException: Message: Screenshot: available via screen
在使用selenium+phantomjs的时候在Windows平台下能够正常工作,在Linux下却不能,并得到错误信息: selenium.common.exceptions.TimeoutExce ...
- html5 文件拖拽上传
本文首先发表在 码蜂笔记 : http://coderbee.net/index.php/web/20130703/266 html5 文件拖拽上传是个老话题了,网上有很多例子,我一开始的代码也是网 ...
- 使用WinINet和WinHTTP实现Http訪问
使用WinINet和WinHTTP实现Http訪问 飘飘白云 l_zhaohui@163.com 2007-11-30 Http訪问有两种方式,GET和POST,就编程来说GET方式相对简单点,它不用 ...
- Git学习(一) 版本号管理工具
Git 是一个分布式版本号控制工具.它的作者 Linus Torvalds 是这样给我们介绍 Git -- The stupid content tracker(傻瓜式的内容跟踪器) 1. Git ...
- oracle恢复被覆盖的存储过程
假设你不小心覆盖了之前的存储过程,那得赶紧闪回,时长越长闪回的可能性越小.原理非常easy,存储过程的定义就是数据字典,改动数据字典跟改动普通表的数据没有差别,此时会把改动前的内容放到undo中,我们 ...
- 浙江大学PAT上机题解析之1014. 福尔摩斯的约会 (20)
1014. 福尔摩斯的约会 (20) 时间限制 50 ms 内存限制 32000 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Y ...
- MySQL Handling of GROUP BY--官方文档
In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in th ...
- Log4j 2.0 使用说明
原文地址:http://blog.csdn.net/welcome000yy/article/details/7962447 Log4j 2.0 使用说明(1) 之HelloWorld 最近刚接触 ...