相关疑难解决方法(0)

如何以递归方式将所有引用加载到AppDomain的程序集?

我想加载到一个新的AppDomain一些具有复杂引用树的程序集(MyDll.dll - > Microsoft.Office.Interop.Excel.dll - > Microsoft.Vbe.Interop.dll - > Office.dll - > stdole.dll)

据我所知,当加载程序集时AppDomain,它的引用不会自动加载,我必须手动加载它们.所以当我这样做时:

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));
Run Code Online (Sandbox Code Playgroud)

得到了FileNotFoundException:

无法加载文件或程序集"MyDll,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null"或其依赖项之一.该系统找不到指定的文件.

我认为关键部分是其依赖性之一.

好的,我之前做过 domain.Load(AssemblyName.GetAssemblyName(path));

foreach (AssemblyName refAsmName in Assembly.ReflectionOnlyLoadFrom(path).GetReferencedAssemblies())
{
    domain.Load(refAsmName);
}
Run Code Online (Sandbox Code Playgroud)

FileNotFoundException又一次,在另一个(参考)集会上.

如何递归加载所有引用?

在加载根程序集之前是否必须创建引用树?如何在不加载程序集的情况下获取程序集的引用?

.net c# reflection assemblies appdomain

108
推荐指数
7
解决办法
12万
查看次数

加载字节数组装配

我正在尝试使用字节数组加载程序集,但我无法弄清楚如何让它正常工作.这是设置:

public static void Main() 
{
    PermissionSet permissions = new PermissionSet(PermissionState.None);
    AppDomainSetup setup = new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory };
    AppDomain friendlyDomain = AppDomain.CreateDomain("Friendly", null, setup, permissions);

    Byte[] primary = File.ReadAllBytes("Primary.dll_");
    Byte[] dependency = File.ReadAllBytes("Dependency.dll_");

    // Crashes here saying it can't find the file.
    friendlyDomain.Load(dependency);

    AppDomain.Unload(friendlyDomain);

    Console.WriteLine("Stand successful");
    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

我创建了两个模拟dll,并故意将其扩展名重命名为".dll_",因此系统无法找到物理文件.双方primarydependency正确填写,但是当我试图调用AppDomain.Load与二进制数据的方法,它回来:

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

c# appdomain

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

appdomain ×2

c# ×2

.net ×1

assemblies ×1

reflection ×1