C# 在appdomain中加载静态类

C# 在appdomain中加载静态类,c#,appdomain,static-class,C#,Appdomain,Static Class,我在C#AppDomain中遇到了一个大问题 我需要在.dll文件中加载静态类并执行其方法: 当我试图通过 Assembly.LoadFrom("XXXXX") // (XXXXX is the full path of dll) .dll将不会自动或以编程方式卸载 当我尝试在AppDomain中加载它们时,如 adapterDomain = AppDomain.CreateDomain("AdapterDomain"); (a)adapterDomain.CreateInstanceFrom

我在C#AppDomain中遇到了一个大问题

我需要在.dll文件中加载静态类并执行其方法:

  • 当我试图通过

    Assembly.LoadFrom("XXXXX") // (XXXXX is the full path of dll)
    
    .dll将不会自动或以编程方式卸载

  • 当我尝试在AppDomain中加载它们时,如

    adapterDomain = AppDomain.CreateDomain("AdapterDomain");
    (a)adapterDomain.CreateInstanceFrom(this.AdapterFilePath, this.AdapterFullName);
    (b)adapterAssembly=adapterDomain.Load(AssemblyName.GetAssemblyName(this.AdapterFilePath));
    
    如果我使用方法(a),因为目标类是静态类,所以它不起作用

    如果我使用方法(b),因为target.dll与我的项目不在同一目录下,我将得到一个异常

  • 如何加载.dll和静态类,然后在使用后卸载.dll?

    方法(b)失败,因为无法解析不在基本应用目录、探测专用路径或GAC中的程序集

    还要注意,
    AppDomain.Load
    而不是在特定AppDomain上加载程序集(如示例代码中的
    adapterDomain.Load
    )。相反,它正在将其加载到当前AppDomain上(这是调用
    AppDomain.Load
    。此行为在上有注释。)显然这不是您要查找的

    下面是一个如何在子AppDomain中调用静态方法的示例:

    class Program
    {
        static void Main(string[] args)
        {
            // This is for testing purposes!
            var loadedAssembliesBefore = AppDomain.CurrentDomain.GetAssemblies();
    
            var domain = AppDomain.CreateDomain("ChildDomain");                        
            // This will make the call to the static method in the dhild AppDomain.
            domain.DoCallBack(LoadAssemblyAndCallStaticMethod);
            // Print the loaded assemblies on the child AppDomain. This is for testing purposes!
            domain.DoCallBack(PrintLoadedAssemblies);
            AppDomain.Unload(domain);
    
            // This is for testing purposes!
            var loadedAssembliesAfter = AppDomain.CurrentDomain.GetAssemblies();
            // Assert that no assembly was leaked to the main AppDomain.
            Debug.Assert(!loadedAssembliesBefore.Except(loadedAssembliesAfter).Any());
    
            Console.ReadKey();
        }
    
        // Loads StaticMethodInHere.dll to the current AppDomain and calls static method 
        // StaticClass.DoSomething.  
        static void LoadAssemblyAndCallStaticMethod()
        {
            var assembly = Assembly.LoadFrom(@"PATH_TO_ASSEMBLY");
    
            assembly.GetType("CLASS_CONTAINING_STATIC_METHOD")
                    .InvokeMember("STATIC_METHOD", 
                                  BindingFlags.Public | 
                                  BindingFlags.Static | 
                                  BindingFlags.InvokeMethod, 
                                  null, 
                                  null, 
                                  null);
        }
    
        // Prints the loaded assebmlies in the current AppDomain. For testing purposes.
        static void PrintLoadedAssemblies()
        {
            Console.WriteLine("/ Assemblies in {0} -------------------------------",
                              AppDomain.CurrentDomain.FriendlyName);
    
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                Console.WriteLine(assembly.FullName);
            }            
        }
    }
    
    要实现此功能,您需要更换:

    • PATH_TO_ASSEMBLY,其中包含静态方法(包括扩展)的程序集的路径
    • 包含静态方法的类的名称,包括该类的命名空间
    • 静态方法的名称

    请注意,
    BindingFlags
    是为公共静态方法设置的。

    您读过了吗?无法卸载DLL。你可以卸载AppDomain,但我不能在DLL中将静态类添加到AppDomain..阅读Josh的链接后,我没有找到解决问题的方法,本文介绍的方法都是关于普通类的,但我想将静态类加载到AppDomainYes,它与你的方法配合得很好,但是还有一件事需要注意:定义AppDomain的方法必须是静态方法,而不必是静态方法。您可以在子AppDomain中创建一个中间对象来进行加载。是 啊根据您的指导和MSDN页面【】第三个案例,我了解到了这一点,非常感谢您