C# 动态加载dll

原文来自 http://wenku.baidu.com/view/3d6c0c4c2b160b4e767fcf30.html

原理如下:

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[]);//根据参数直线方法,返回值就是原方法的返回值

 

[c-sharp] view plain copy print ?
  1. #region 声明动态载入DLL的参数   
  2.        object obj=null;  
  3.        byte[] filesByte;  
  4.        Assembly assembly;  
  5.        Type type;  
  6.        MethodInfo timerInitial;  
  7.        MethodInfo timerDispose;  
  8.        #endregion   
  9.   
  10.     private void LoadDll()//加载DLL   
  11.        {  
  12.            try  
  13.            {  
  14.                filesByte = File.ReadAllBytes(Path.GetDirectoryName(Application.ExecutablePath) + "//loadDll.dll");  
  15.                assembly = Assembly.Load(filesByte);  
  16.                type = assembly.GetType("test.loadDll");  
  17.                obj = System.Activator.CreateInstance(type);  
  18.                timerStart = tp.GetMethod("TimerStart");  
  19.                timerStop = tp.GetMethod("TimerStop");  
  20.                if (timerStart != null)  
  21.                {  
  22.                    timerStart.Invoke(obj, null);  
  23.                }  
  24.            }  
  25.            catch(Exception)  
  26.            {  
  27.   
  28.            }  
  29.        }  

 

以下摘自MSDN

[c-sharp] view plain copy print ?
  1. public class A  
  2. {  
  3.     public virtual int method () {return 0;}  
  4. }  
  5.   
  6. public class B  
  7. {  
  8.     public virtual int method () {return 1;}  
  9. }  
  10.   
  11. class Mymethodinfo  
  12. {  
  13.     public static int Main()  
  14.     {  
  15.         Console.WriteLine ("/nReflection.MethodInfo");  
  16.         A MyA = new A();  
  17.         B MyB = new B();  
  18.   
  19.         // Get the Type and MethodInfo.  
  20.         Type MyTypea = Type.GetType("A");  
  21.         MethodInfo Mymethodinfoa = MyTypea.GetMethod("method");  
  22.   
  23.         Type MyTypeb = Type.GetType("B");  
  24.         MethodInfo Mymethodinfob = MyTypeb.GetMethod("method");  
  25.   
  26.         // Get and display the Invoke method.  
  27.         Console.Write("/nFirst method - " + MyTypea.FullName +  
  28.             " returns " + Mymethodinfoa.Invoke(MyA, null));  
  29.         Console.Write("/nSecond method - " + MyTypeb.FullName +  
  30.             " returns " + Mymethodinfob.Invoke(MyB, null));  
  31.         return 0;  
  32.     }  
  33. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值