c#实现动态加载Dll
来源:互联网 发布:数据库软件access 编辑:程序博客网 时间:2024/05/01 03:19
基本思路:
1、利用反射进行动态加载和调用.
Assembly assembly=Assembly.LoadFrom(DllPath); //利用dll的路径加载,同时将此程序集所依赖的
程序集加载进来,需后辍名.dll
Assembly.LoadFile 只加载指定文件,并不会自动加载依赖程序集.Assmbly.Load无需后辍名
2、加载dll后,需要使用dll中某类.
Type type=ass.GetType(“TypeName”);//用类型的命名空间和名称获得类型
3、需要实例化类型,才可以使用,参数可以人为的指定,也可以无参数,静态实例可以省略
Object obj = Activator.CreateInstance(type,params[]);//利用指定的参数实例话类型
4、调用类型中的某个方法:
需要首先得到此方法
MethodInfo mi=type.GetMethod(“MehtodName”);//通过方法名称获得方法
5、然后对方法进行调用,多态性利用参数进行控制
mi.Invoke(obj,params[]);//根据参数直线方法,返回值就是原方法的返回值
例子:
private void LoadDll()//加载DLL
{
try
{
//先把文件读进来
byte[] filesByte = File.ReadAllBytes(Path.GetDirectoryName(Application.ExecutablePath) + "//XXXDll.dll");
Assembly assembly = Assembly.Load(filesByte);
Type type = assembly.GetType("stone.testDll"); //记得把命名空间加上
object obj = System.Activator.CreateInstance(type);
MethodInfo uStart = tp.GetMethod("uStart");
MethodInfo uStop = tp.GetMethod("uStop");
if (timerStart != null)
{
timerStart.Invoke(obj, null);
}
}
catch(Exception)
{
}
}
以下摘自MSDN
public class A
{
public virtual int method () {return 0;}
}
public class B
{
public virtual int method () {return 1;}
}
class Mymethodinfo
{
public static int Main()
{
Console.WriteLine ("/nReflection.MethodInfo");
A MyA = new A();
B MyB = new B();
// Get the Type and MethodInfo.
Type MyTypea = Type.GetType("A");
MethodInfo Mymethodinfoa = MyTypea.GetMethod("method");
Type MyTypeb = Type.GetType("B");
MethodInfo Mymethodinfob = MyTypeb.GetMethod("method");
// Get and display the Invoke method.
Console.Write("/nFirst method - " + MyTypea.FullName +
" returns " + Mymethodinfoa.Invoke(MyA, null));
Console.Write("/nSecond method - " + MyTypeb.FullName +
" returns " + Mymethodinfob.Invoke(MyB, null));
return 0;
}
}
0 0
- c#实现动态加载Dll
- c#实现动态加载Dll
- c#实现动态加载Dll
- c#实现动态加载Dll
- c#实现动态加载Dll
- c#实现动态加载Dll
- C#中实现动态加载dll库
- 转载: c#实现动态加载Dll 收藏
- C#通过反射实现动态加载DLL
- C#动态加载DLL
- C#动态加载DLL
- C#动态加载DLL
- C#动态加载DLL
- C#动态加载DLL
- C#动态加载DLL
- C#动态加载DLL
- C#动态加载DLL
- C# 动态加载dll
- PAT 1014 Waiting in Line (30)
- 非常有用的101道算法部分常见面试题
- linux--文件--系统文件和目录 /dev
- 传统的Dialog对话框
- 项目图片
- c#实现动态加载Dll
- 最全的资源教程-前端涉及的所有知识体系
- Java实现QPS控制
- Oracle学习笔记【01】
- mysql字符函数简析
- 图片压缩小例子
- 140个Google面试问题
- HDOJ 1754 I Hate It (线段树单点更新求区间最大值)
- Visualizing and Understanding Convolutional Networks笔记