最近由于业务要求,需要动态调DLL类库,所以研究了一下,感觉还好也不太难,今天就把自己理解一个小例子(已经通过VS2005跑通),供大家一起研究探讨,有理解不当地方还请高手们多多指正,谢谢啦!

好,在这之前我先把反射所需要使几个类给大家列一下:

1、使Assembly类定义加载程序集,加载在程序集清单中列出模块,以及从此程序集中查找类型并创建该类型实例。

2、使MethodInfo了解方法名称、返回类型、参数、访问修饰符(如pulic 或private)实现详细信息(如abstract或virtual)等。使TypeGetMethods或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类库的更多相关文章

  1. C# 通过反射类动态调用DLL方法

    网上看了很多关于反射的思路和方法,发现这个还算不错 //使用反射方: using System; using System.Collections.Generic; using System.Linq ...

  2. C#程序实现动态调用DLL的研究(转)

    摘 要:在<csdn开发高手>2004年第03期中的<化功大法——将DLL嵌入EXE>一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在可执行文件运行时,自动从资 ...

  3. 用C#通过反射实现动态调用WebService 告别Web引用

    我们都知道,调用WebService可以在工程中对WebService地址进行WEB引用,但是这确实很不方便.我想能够利用配置文件灵活调用WebService.如何实现呢? 用C#通过反射实现动态调用 ...

  4. C#程序实现动态调用DLL的研究[转]

    摘   要: 在< csdn 开发高手> 2004 年第 03 期中的<化功大法——将 DLL 嵌入 EXE >一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在 ...

  5. C#程序实现动态调用DLL的研究

    摘 要:在<csdn开发高手>2004年第03期中的<化功大法——将DLL嵌入EXE>一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在可执行文件运行时,自动从资 ...

  6. 用C#通过反射实现动态调用WebService 告别Web引用(转载)

    我们都知道,调用WebService可以在工程中对WebService地址进行WEB引用,但是这确实很不方便.我想能够利用配置文件灵活调用WebService.如何实现呢? 用C#通过反射实现动态调用 ...

  7. Java反射机制动态代理

    1.什么事反射机制动态代理 在一段代码的前后动态执行其他操作,比如有一个方法是往数据库添加一个记录,我们可以通过动态代理,在操作数据库方法的前和后添加代码执行打开数据库连接和关闭数据库连接. 2.演示 ...

  8. C++利用模板在Windows上快速调用DLL函数

    更新日志 --------- 2021/08/01 更新V2.2 增加 GetHmodule 函数 - 允许用户获取HMODULE以验证加载DLL是否成功. 2021/08/03 更新V2.3 增加 ...

  9. 卸载AppDomain动态调用DLL异步线程执行失败

    应用场景 动态调用DLL中的类,执行类的方法实现业务插件功能 使用Assembly 来实现 但是会出现逻辑线程数异常的问题 使用AppDomain 实现动态调用,并卸载. 发现问题某个插件中开启异步线 ...

随机推荐

  1. [转]让程序在崩溃时体面的退出之SEH+Dump文件

    原文地址:http://blog.csdn.net/starlee/article/details/6649605 在我上篇文章<让程序在崩溃时体面的退出之SEH>中讲解了SEH中try/ ...

  2. poj 1696 Space Ant 极角排序

    #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #inclu ...

  3. java 检查抛出的异常是否是要捕获的检查性异常或运行时异常或错误

    /** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeExcepti ...

  4. C#三种判断数据库中取出的字段值是否为空(NULL) 的方法

    操作数据库,需要判断返回的字段值是否为空,收集了3种方法供参考 1 通过System.DBNull判断,网上大部分都使用这个方法. DataTable dt;                     ...

  5. php session already send by ……

    初学者在处理登录注册的时候可能会遇到一个问题就是Warning: Cannot modify header information - headers already sent by .... 这是什 ...

  6. ASP.NET通过http/https的POST方式,发送和接受XML文件内容

    本文转载:http://hi.baidu.com/ysyhyt/item/5011ae39ce3cf49fb80c0395 本文参考:http://blog.csdn.net/ououou123456 ...

  7. BFS-hdu-4101-Ali and Baba

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4101 题目大意: 给一个矩阵,0表示空的可走,-1宝藏的位置(只有一个),其余的正整数表示该位置石头 ...

  8. [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)

    Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. jQuery支持mobile的全屏水平横向翻页效果

    这是一款支持移动手机mobile设备的jQuery全屏水平横向翻页效果插件. 该翻页插件能够使页面在水平方向上左右全屏翻动,它支持手机触摸屏,支持使用鼠标滚动页面. 整个页面过渡平滑,效果很不错. 在 ...

  10. how tomcat works 读书笔记四 tomcat的默认连接器

    事实上在第三章,就已经有了连接器的样子了,只是那仅仅是一个学习工具,在这一章我们会開始分析tomcat4里面的默认连接器. 连接器 Tomcat连接器必须满足下面几个要求 1 实现org.apache ...