当我设置 DisallowApplicationBaseProbing = true 时,我需要在我创建的 AppDomain 上连接一个 AssemblyResolve 事件。我这样做的原因是强制运行时调用它需要解析程序集的 AssemblyResolve 事件,而不是先探测。这样,另一个开发人员不能只将 MyDllName.dll 粘贴在 ApplicationBase 目录中并覆盖我想在 AssemblyResolve 事件中加载的程序集。
这样做的问题如下......
class Program
{
static void Main()
{
AppDomainSetup ads = new AppDomainSetup();
ads.DisallowApplicationBaseProbing = true;
AppDomain appDomain = AppDomain.CreateDomain("SomeDomain", null, ads);
appDomain.AssemblyResolve += OnAssemblyResolve;
appDomain.DoCallBack(target);
}
static System.Reflection.Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
Console.WriteLine("Hello");
return null;
}
private static void target()
{
Console.WriteLine(AppDomain.CurrentDomain);
}
}
代码永远不会超过 += OnAssemblyResolve 行。
当代码尝试执行时,新的应用程序域会尝试解析我正在执行的程序集。因为 DisallowApplicationBaseProbing = true,它不知道在哪里可以找到该程序集。我似乎有鸡和蛋的问题。它需要解析我的程序集以连接程序集解析器,但需要程序集解析器来解析我的程序集。
感谢您的任何帮助。
-麦克风