0

我尝试了下一个代码:

AppDomain ad = AppDomain.CreateDomain("Test");      
_Assembly = parDomain.Load(AssemblyName.GetAssemblyName(@"C:\SomeDLLPath\PhysicsTest.dll"));
// Some work with assembly
AppDomain.Unload(ad);

它提高FileNotFoundException that cannot load file or assembly "TestClass, Version=1.0.0.0, ..."

如果我将程序集加载到该域一切正常:

_Assembly = Assembly.LoadFile(@"C:\SomeDLLPath\PhysicsTest.dll");

但我也需要卸载这个。

我看到很多关于它的线程,但无法理解它们......

4

1 回答 1

1

来自MSDN

块引用

如果不卸载包含它的所有应用程序域,就无法卸载单个程序集。即使程序集超出范围,实际的程序集文件仍将保持加载,直到包含它的所有应用程序域都被卸载。

这是卸载 AppDomain MSDN的方法

using System;
using System.Reflection;

class AppDomain2
{
    public static void Main()
    {
        Console.WriteLine("Creating new AppDomain.");
        AppDomain domain = AppDomain.CreateDomain("MyDomain", null);

        Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
        Console.WriteLine("child domain: " + domain.FriendlyName);
        AppDomain.Unload(domain);
        try
        {
            Console.WriteLine();
            Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
            // The following statement creates an exception because the domain no longer exists.
            Console.WriteLine("child domain: " + domain.FriendlyName);
        }
        catch (AppDomainUnloadedException e)
        {
            Console.WriteLine(e.GetType().FullName);
            Console.WriteLine("The appdomain MyDomain does not exist.");
        }
    }
}
于 2012-05-13T19:32:42.760 回答