C#中使用反射动态加载和卸载DLL

C++中加载和卸载DLL是一件很容易的事,LoadLibrary和FreeLibrary让你能够轻 易的在程序中加载DLL,然后在任何地方卸载。在C#中我们也能使用Assembly.LoadFile实现动态加载DLL,但是当你试图卸载时,你会很 惊讶的发现Assembly没有提供任何卸载的方法。这是由于托管代码的自动垃圾回收机制会做这件事情,所以C#不提供释放资源的函数,一切由垃圾回收来 做。这引发了一个问题,用Assembly加载的DLL可能只在程序结束的时候才会被释放,这也意味着在程序运行期间无法更新被加载的DLL。而这个功能 在某些程序设计时是非常必要的,考虑你正在用反射机制写一个查看DLL中所有函数详细信息的程序,程序提供一个菜单让用户可以选择DLL文件,这时就需要 让程序能够卸载DLL,否则一旦用户重新得到新版本DLL时,必须要重新启动程序,重新选择加载DLL文件,这样的设计是用户无法忍受的。 C#也提供了实现动态卸载DLL的方法,通过AppDomain来实现。AppDomain是一个独立执行应用程序的环境, 当 AppDomain被卸载的时候,在该环境中的所有资源也将被回收 。关于AppDomain的详细资料参考MSDN。下面是使用 AppDomain实现动态卸载DLL的代码,
 
  1. using  System;  
  2.   
  3. using  System.Collections.Generic;  
  4.   
  5. using  System.Text;  
  6.   
  7. using  System.Threading;  
  8.   
  9. using  System.Reflection;  
  10.   
  11. namespace  UnloadDll  
  12.   
  13. {  
  14.   
  15.     class  Program  
  16.   
  17.     {  
  18.   
  19.         static   void  Main( string [] args)  
  20.   
  21.         {  
  22.   
  23.             string  callingDomainName = AppDomain.CurrentDomain.FriendlyName; //Thread.GetDomain().FriendlyName;   
  24.   
  25.             Console.WriteLine(callingDomainName);  
  26.   
  27.             AppDomain ad = AppDomain.CreateDomain("DLL Unload test" );  
  28.   
  29.             ProxyObject obj = (ProxyObject)ad.CreateInstanceFromAndUnwrap(@"UnloadDll.exe""UnloadDll.ProxyObject" );  
  30.   
  31.             obj.LoadAssembly();  
  32.   
  33.             obj.Invoke("TestDll.Class1""Test""It's a test" );  
  34.   
  35.             AppDomain.Unload(ad);  
  36.   
  37.             obj = null ;  
  38.   
  39.             Console.ReadLine();  
  40.   
  41.         }  
  42.   
  43.     }  
  44.   
  45.     class  ProxyObject : MarshalByRefObject  
  46.   
  47.     {  
  48.   
  49.         Assembly assembly = null ;  
  50.   
  51.         public   void  LoadAssembly()  
  52.   
  53.         {  
  54.   
  55.             assembly = Assembly.LoadFile(@"TestDLL.dll" );              
  56.   
  57.         }  
  58.   
  59.         public   bool  Invoke( string  fullClassName,  string  methodName,  params  Object[] args)  
  60.   
  61.         {  
  62.   
  63.             if (assembly ==  null )  
  64.   
  65.                 return   false ;  
  66.   
  67.             Type tp = assembly.GetType(fullClassName);  
  68.   
  69.             if  (tp ==  null )  
  70.   
  71.                 return   false ;  
  72.   
  73.             MethodInfo method = tp.GetMethod(methodName);  
  74.   
  75.             if  (method ==  null )   
  76.   
  77.                 return   false ;  
  78.   
  79.             Object obj = Activator.CreateInstance(tp);  
  80.   
  81.             method.Invoke(obj, args);  
  82.   
  83.             return   true ;              
  84.   
  85.         }  
  86.   
  87.     }  
  88.   
  89. }  
using System;

using System.Collections.Generic;

using System.Text;

using System.Threading;

using System.Reflection;

namespace UnloadDll

{

    class Program

    {

        static void Main(string[] args)

        {

            string callingDomainName = AppDomain.CurrentDomain.FriendlyName;//Thread.GetDomain().FriendlyName;

            Console.WriteLine(callingDomainName);

            AppDomain ad = AppDomain.CreateDomain("DLL Unload test");

            ProxyObject obj = (ProxyObject)ad.CreateInstanceFromAndUnwrap(@"UnloadDll.exe", "UnloadDll.ProxyObject");

            obj.LoadAssembly();

            obj.Invoke("TestDll.Class1", "Test", "It's a test");

            AppDomain.Unload(ad);

            obj = null;

            Console.ReadLine();

        }

    }

    class ProxyObject : MarshalByRefObject

    {

        Assembly assembly = null;

        public void LoadAssembly()

        {

            assembly = Assembly.LoadFile(@"TestDLL.dll");            

        }

        public bool Invoke(string fullClassName, string methodName, params Object[] args)

        {

            if(assembly == null)

                return false;

            Type tp = assembly.GetType(fullClassName);

            if (tp == null)

                return false;

            MethodInfo method = tp.GetMethod(methodName);

            if (method == null) 

                return false;

            Object obj = Activator.CreateInstance(tp);

            method.Invoke(obj, args);

            return true;            

        }

    }

}

注意: 1. 要想让一个对象能够穿过AppDomain边界,必须要继承MarshalByRefObject类,否则无法被其他AppDomain使用。 2. 每个线程都有一个默认的AppDomain,可以通过Thread.GetDomain()来得到

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值