C# 動態載入 動態卸載

來源:互聯網
上載者:User
代碼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;
        }
    }
}

在C++中載入和卸載DLL是一件很容易的事,LoadLibrary和FreeLibrary讓你能夠輕易的在程式中載入DLL,然後在任何地方卸載。 在C#中我們也能使用Assembly.LoadFile實現動態載入DLL,但是當你試圖卸載時,你會很驚訝的發現Assembly沒有提供任何卸載的 方法。這是由於Managed 程式碼的自動記憶體回收機制會做這件事情,所以C#不提供釋放資源的函數,一切由記憶體回收來做。

這引 發了一個問題,用Assembly載入的DLL可能只在程式結束的時候才會被釋放,這也意味著在程式運行期間無法更新被載入的DLL。而這個功能在某些程 序設計時是非常必要的,考慮你正在用反射機制寫一個查看DLL中所有函數詳細資料的程式,程式提供一個菜單讓使用者可以選擇DLL檔案,這時就需要讓程式能 夠卸載DLL,否則一旦使用者重新得到新版本DLL時,必須要重新啟動程式,重新選擇載入DLL檔案,這樣的設計是使用者無法忍受的。

C# 也提供了實現動態卸載DLL的方法,通過AppDomain來實現。AppDomain是一個獨立執行應用程式的環境,當AppDomain被卸載的時 候,在該環境中的所有資源也將被回收。關於AppDomain的詳細資料參考MSDN。下面是使用AppDomain實現動態卸載DLL的代碼

 

注意:

1. 要想讓一個對象能夠穿過AppDomain邊界,必須要繼承MarshalByRefObject類,否則無法被其他AppDomain使用。

2. 每個線程都有一個預設的AppDomain,可以通過Thread.GetDomain()來得到  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.