2015-04-16

 

Assembly没有Unload方法,那么程序如何在运行状态下卸载程序集呢?

可以用AppDomain.Unload方法。

想法一 (失败):

  1. create new Domain
  2. load assembly in new domain
  3. 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 }
View Code

相关文章:

  • 2021-06-29
  • 2022-12-23
  • 2021-10-11
  • 2022-02-04
  • 2022-01-08
  • 2021-08-05
猜你喜欢
  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
  • 2022-01-06
相关资源
相似解决方案