将程序集加载到新的应用程序域中,但不加载到CurrentDomain中 [英] Load Assembly into a new app domain but not CurrentDomain

查看:92
本文介绍了将程序集加载到新的应用程序域中,但不加载到CurrentDomain中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题围绕着节省内存。

So my problem revolves around saving memory.

本质上,我需要将程序集加载到除主/当前域之外的单独的应用程序域中,检查该程序集中的类型,然后卸载新域完成后。

In essence I need to load an assembly in to a separate app domain other than the main/current domain, check for types within that assembly, and then unload the new domain when done.

当前我的解决方案如下:

Currently my solution is as follows:

AppDomain NewDomain = AppDomain.CreateDomain("newdomain");

foreach(string path in dllPaths) //string list of dll paths
{
    byte[] dllBytes = File.ReadAllBytes(dll);
    NewDomain.Load(dllBytes); //offending line
}

DoStuffWithNewDomain();
AppDomain.Unload(NewDomain);

NewDomain.Load行似乎将程序集加载到新域中,也加载到当前域中我的程序。

The NewDomain.Load line seems to load the assembly into the new domain but also into the current domain of my program.

我使用此链接作为参考- http://www.csharp411.com/how-to-load-a-net-assembly-进入一个单独的应用程序域,以便您可以卸载它/

I used this link as a reference - http://www.csharp411.com/how-to-load-a-net-assembly-into-a-separate-appdomain-so-you-can-unload-it/

非常感谢:)

推荐答案

如前所述,仅对于当前应用程序域,才可以从字节加载程序集。

As mentioned already, loading the assembly from bytes can only happen for the current app domain.

此处是一种在应用程序域上使用 DoCallBack 方法进行上下文切换以使目标应用程序域为当前域的方法( http://msdn.microsoft.com/en-us/library/system。 appdomain.docallback%28v = vs.110%29.aspx )。

Here is one way to context-switch to make your target app domain the current one, using the DoCallBack method on the app domain (http://msdn.microsoft.com/en-us/library/system.appdomain.docallback%28v=vs.110%29.aspx).

在加载程序集之后,您可以使用相同的方法检查新应用程序域的程序集和类型,并在卸载之前根据需要创建实例。

After the assemblies are loaded you can then use the same method to inspect the assemblies and types of the new app domain, and create instances as necessary, before unloading it.

class Program
{
    static void Main(string[] args)
    {
        AppDomain newDomain = AppDomain.CreateDomain("NewDomain");
        List<string> dllPaths = new List<string>() { @"c:\dev\taglib-sharp.dll" };

        foreach (string dll in dllPaths)
        {
            AppDomainAsmLoader asmLoad = new AppDomainAsmLoader(File.ReadAllBytes(dll));
            newDomain.DoCallBack(new CrossAppDomainDelegate(asmLoad.LoadAsm));
        }

        newDomain.DoCallBack(new CrossAppDomainDelegate(DoWorkWithAppDomain));

        AppDomain.Unload(newDomain);
        Console.ReadKey();
    }

    public static void DoWorkWithAppDomain()
    {
        Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies();
        foreach (Assembly asm in asms)
        {
            Type[] types = asm.GetTypes();
            foreach (Type type in types)
            {
                Console.WriteLine("Found the type: {0}", type.FullName);
            }
        }
    }

    [Serializable]
    public class AppDomainAsmLoader
    {
        private byte[] AsmData;  

        public AppDomainAsmLoader(byte[] data)
        {
            AsmData = data;
        }         

        public void LoadAsm()
        {
            Assembly asm = Assembly.Load(AsmData);
        }

    }
}

这篇关于将程序集加载到新的应用程序域中,但不加载到CurrentDomain中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆