c# - C# - 无法从创建的域中获取程序集
问题描述
我创建了一个新的应用程序域并将 2 个程序集加载到其中(参见下面的代码示例)。但是,如果我尝试使用GetAssemblies
方法,我会得到异常System.IO.FileNotFoundException
。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
namespace TestAppDomains
{
class Program
{
static void Main(string[] args)
{
String path = @"D:\PROGRAMMING\ConsoleTest\ConsoleTest\bin\Debug\ConsoleTest.exe";
String pathdll = @"D:\PROGRAMMING\ConsoleTestDll\ConsoleTestDll\bin\Debug\ConsoleTestDll.dll";
AppDomain myDomain1 = AppDomain.CreateDomain("Domain1");
//Create the loader (a proxy).
var assemblyLoader = (SimpleAssemblyLoader)myDomain1.CreateInstanceAndUnwrap(typeof(SimpleAssemblyLoader).Assembly.FullName, typeof(SimpleAssemblyLoader).FullName);
assemblyLoader.LoadFrom(path);
assemblyLoader.LoadFrom(pathdll);
var currentDomainAssemblies = AppDomain.CurrentDomain.GetAssemblies();
var myDomainAssemblies = myDomain1.GetAssemblies(); //There is exception System.IO.FileNotFoundException
Task.Run(() => myDomain1.ExecuteAssembly(path));
//Finally unload the AppDomain.
Console.ReadKey();
AppDomain.Unload(myDomain1);
}
public class SimpleAssemblyLoader : MarshalByRefObject
{
public void Load(string path)
{
ValidatePath(path);
Assembly.Load(path);
}
public void LoadFrom(string path)
{
ValidatePath(path);
Assembly.LoadFrom(path);
}
private void ValidatePath(string path)
{
if (path == null) throw new ArgumentNullException("path");
if (!System.IO.File.Exists(path))
throw new ArgumentException(String.Format("path \"{0}\" does not exist", path));
}
}
}
}
解决方案
推荐阅读
- php - 我们可以通过 ajax 请求劫持 CSRF 令牌吗
- javascript - 如何从循环中制作多维数组
- python - 来自导入脚本的 Python 方法无法识别
- r - 比较 nlme 和 lme4 之间随机截距和斜率无关的脚本
- java - 如何自动关注列表的当前元素(Android App)
- php - 我没有收到来自报价单的任何电子邮件
- react-native - 找不到模块“eslint-plugin-prettier”
- c# - 在文本块中使用资源变量
- react-native - 任务':react-native-multi-image-selector:compileReleaseJavaWithJavac'在本机反应中执行失败
- python - 如何将列表(不是 2D)顺时针旋转 90 度?