簡體   English   中英

從C#中的文件將DLL程序集加載到自定義AppDomain

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

我嘗試下一個代碼:

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

但是我也需要卸載它。

我看到了很多關於它的話題,但聽不懂...

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