c#中通過反射可以方便的動態加載dll程序集,但是如果你需要對dll進行更新,卻發現.net類庫沒有提供卸載dll程序集的方法。在.net 中,加入了應用程序域的概念,應用程序域是可以卸載的。也就是說,如果需要對動態加載的dll程序集進行更新,可以通過以下方法解決:
新建一個應用程序域,在該應用程序域中動態加載DLL,然后可以卸載掉該應用程序域。該應用程序域被卸載的時候,相關資源也會被回收。
要想這樣實現,就要讓你程序的currentDomain和新建的newDomain之間進行通信,穿過應用程序域的邊界。從網上找到了某大牛的解決方法,抄下來留給自己看吧:
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()來得到