【发布时间】:2013-07-24 13:57:21
【问题描述】:
我正在尝试仅使用字节数组加载程序集,但我不知道如何让它正常工作。这是设置:
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();
}
我创建了两个模拟 dll,并故意将它们的扩展名重命名为“.dll_”,这样系统就无法找到物理文件。 primary 和 dependency 都正确填写,但是当我尝试使用二进制数据调用 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.
为什么要在系统中搜索文件?
更新
另一方面,这似乎有效:
public class Program {
public static void Main() {
PermissionSet permissions = new PermissionSet(PermissionState.Unrestricted);
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(primary);
Stage stage = (Stage)friendlyDomain.CreateInstanceAndUnwrap(typeof(Stage).Assembly.FullName, typeof(Stage).FullName);
stage.LoadAssembly(dependency);
Console.WriteLine("Stand successful");
Console.ReadLine();
}
}
public class Stage : MarshalByRefObject {
public void LoadAssembly(Byte[] data) {
Assembly.Load(data);
}
}
所以看来AppDomain.Load 和Assembly.Load 之间存在差异。
【问题讨论】:
-
依赖 DLL 是否有任何尚未复制的依赖项?
-
主要依赖依赖。依赖关系虽然没有(非 CLR)依赖关系。似乎运行时不应该开始搜索文件。