博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c#实现动态加载Dll(程序集)

Posted on 2011-10-11 13:43  Honor  阅读(1567)  评论(0)    收藏  举报

原理如下:

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

  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.         }  
#region 声明动态载入DLL的参数 object obj=null; byte[] filesByte; Assembly assembly; Type type; MethodInfo timerInitial; MethodInfo timerDispose; #endregion private void LoadDll()//加载DLL { try { filesByte = File.ReadAllBytes(Path.GetDirectoryName(Application.ExecutablePath) + "\\loadDll.dll"); assembly = Assembly.Load(filesByte); type = assembly.GetType("test.loadDll"); obj = System.Activator.CreateInstance(type); timerStart = tp.GetMethod("TimerStart"); timerStop = tp.GetMethod("TimerStop"); if (timerStart != null) { timerStart.Invoke(obj, null); } } catch(Exception) { } }

以下摘自MSDN

  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. }