简体   繁体   English

从C#中的文件将DLL程序集加载到自定义AppDomain

[英]Load DLL Assemblies from file in C# to custom AppDomain

I tryed next code: 我尝试下一个代码:

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

It raise FileNotFoundException that cannot load file or assembly "TestClass, Version=1.0.0.0, ..." 它引发了FileNotFoundException that cannot load file or assembly "TestClass, Version=1.0.0.0, ..."

If I Load Assembly to this domain all OK: 如果我将程序集加载到此域,则一切正常:

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

But i need to unload this too. 但是我也需要卸载它。

I saw many threads about it but can't understand them... 我看到了很多关于它的话题,但听不懂...

From MSDN MSDN

Blockquote 块引用

There is no way to unload an individual assembly without unloading all of the application domains that contain it. 如果不卸载包含该程序集的所有应用程序域,则无法卸载该程序集。 Even if the assembly goes out of scope, the actual assembly file will remain loaded until all application domains that contain it are unloaded. 即使程序集超出范围,实际的程序集文件也将保持加载状态,直到包含该程序集的所有应用程序域都被卸载为止。

Here is how to unload a AppDomain 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.");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM