如何加载大会的AppDomain与递归所有引用? [英] How to Load an Assembly to AppDomain with all references recursively?

查看:191
本文介绍了如何加载大会的AppDomain与递归所有引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要加载到一个新的AppDomain一些组件,它有一个复杂的引用树(MYDLL.DLL - > Microsoft.Office.Interop.Excel.dll - > Microsoft.Vbe.Interop.dll - > Office.dll - > stdole.dll)

I want to load to a new AppDomain some assembly which has a complex references tree (MyDll.dll -> Microsoft.Office.Interop.Excel.dll -> Microsoft.Vbe.Interop.dll -> Office.dll -> stdole.dll)

据我了解,当一个程序集被加载到AppDomain中,它的引用将不会自动加载,我要手动加载它们。 所以,当我做的:

As far as I understood, when an assembly is been loaded to AppDomain, its references would not be loaded automatically, and I have to load them manually. So when I do:

string dir = @"SomePath"; // different from AppDomain.CurrentDomain.BaseDirectory
string path = System.IO.Path.Combine(dir, "MyDll.dll");

AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
setup.ApplicationBase = dir;
AppDomain domain = AppDomain.CreateDomain("SomeAppDomain", null, setup);

domain.Load(AssemblyName.GetAssemblyName(path));

和使用了 FileNotFoundException异常

无法加载文件或程序集MYDLL,版本= 1.0.0.0,文化=中性公钥= null或它的一个依赖。该系统找不到指定的文件。

Could not load file or assembly 'MyDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

我认为最关键的部分是一个依赖

I think the key part is one of its dependencies.

好吧,我下一步要做之前 domain.Load(AssemblyName.GetAssemblyName(路径));

Ok, I do next before domain.Load(AssemblyName.GetAssemblyName(path));

foreach (AssemblyName refAsmName in Assembly.ReflectionOnlyLoadFrom(path).GetReferencedAssemblies())
{
    domain.Load(refAsmName);
}

但得到 FileNotFoundException异常再次,在另一个(引用)组装。

But got FileNotFoundException again, on another (referenced) assembly.

如何加载所有引用递归?

How to load all references recursively?

我必须加载根组装之前创建引用树?如何获得程序集的引用,而无需装载了吗?

Do I have to create references tree before loading root assembly? How to get an assembly's references without loading it?

推荐答案

您需要调用 CreateInstanceAndUnwrap 之前,你的代理对象将在国外的应用程序域中执行。

You need to invoke CreateInstanceAndUnwrap before your proxy object will execute in the foreign application domain.

 class Program
{
    static void Main(string[] args)
    {
        AppDomainSetup domaininfo = new AppDomainSetup();
        domaininfo.ApplicationBase = System.Environment.CurrentDirectory;
        Evidence adevidence = AppDomain.CurrentDomain.Evidence;
        AppDomain domain = AppDomain.CreateDomain("MyDomain", adevidence, domaininfo);

        Type type = typeof(Proxy);
        var value = (Proxy)domain.CreateInstanceAndUnwrap(
            type.Assembly.FullName,
            type.FullName);

        var assembly = value.GetAssembly(args[0]);
        // AppDomain.Unload(domain);
    }
}

public class Proxy : MarshalByRefObject
{
    public Assembly GetAssembly(string assemblyPath)
    {
        try
        {
            return Assembly.LoadFile(assemblyPath);
        }
        catch (Exception)
        {
            return null;
            // throw new InvalidOperationException(ex);
        }
    }
}

另外请注意,如果你使用 LoadFrom ,你可能会得到一个 FileNotFound 例外,因为大会解析器会试图找到你正在加载在GAC或当前应用程序的bin文件夹中的程序集。使用的LoadFile 来加载任意的汇编文件,而不是 - 但请注意,如果你这样做,你需要加载任何依赖自己

Also, note that if you use LoadFrom you'll likely get a FileNotFound exception because the Assembly resolver will attempt to find the assembly you're loading in the GAC or the current application's bin folder. Use LoadFile to load an arbitrary assembly file instead--but note that if you do this you'll need to load any dependencies yourself.

这篇关于如何加载大会的AppDomain与递归所有引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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