2015-04-16
Assembly没有Unload方法,那么程序如何在运行状态下卸载程序集呢?
可以用AppDomain.Unload方法。
想法一 (失败):
- create new Domain
- load assembly in new domain
- unload the new Domain
代码如下:
1 using System; 2 3 using System.Reflection; 4 5 namespace UnloadAssembly 6 { 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 string file1 = @"C:\code\UnloadAssembly\Assembly1\bin\Debug\Assembly1.dll"; 12 AppDomain appD = AppDomain.CreateDomain("#D1"); 13 AssemblyName myAssemblyName1 = AssemblyName.GetAssemblyName(file1); 14 System.IO.File.Copy(file1, GetDestinateFile(file1), true); 15 appD.Load(myAssemblyName1); 16 string dName = appD.FriendlyName; 17 AppDomain.Unload(appD); 18 } 19 20 public static string GetDestinateFile(string file) 21 { 22 return Environment.CurrentDirectory + @"\" + new System.IO.FileInfo(file).Name; 23 } 24 } 25 }