【问题标题】:Load DLL Assemblies from file in C# to custom AppDomain将 DLL 程序集从 C# 中的文件加载到自定义 AppDomain
【发布时间】:2012-05-13 18:47:59
【问题描述】:

我尝试了下一个代码:

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");

但我也需要卸载它。

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

【问题讨论】:

    标签: c# reflection dll assemblies


    【解决方案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.");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多