所以最近我一直在研究一个项目,其中应用程序(或可执行文件,无论你想怎么称呼它)需要能够加载和卸载在可执行文件文件夹中根本找不到的程序集。(甚至可能是另一个驱动器)
举个例子,我希望我的应用程序位于D:\AAA\theAppFolder,DLL 文件的程序集位于C:\BBB\Assemblies
仔细看,我发现AppDomain允许卸载自己和任何附加的程序集,所以我想我会试一试,但是经过几个小时的尝试后似乎出现了一个问题:AppDomains 无法查看应用基础。
根据 AppDomain 的纪录片(和我自己的经验),您不能在 ApplicationBase 之外设置 PrivateBinPath ,如果我将 ApplicationBase 设置在应用程序所在的驱动器之外(通过 AppDomainSetup),我会收到System.IO.FileNotFoundException抱怨它不能找到应用程序本身。
因此,我什至无法达到可以使用 AssemblyResolve ResolveEventHandler 尝试使用 MarhsalByRefObject 继承类来获取程序集的阶段......
这是与我目前正在尝试的相关的一些代码片段
internal class RemoteDomain : MarshalByRefObject
{
public override object InitializeLifetimeService() //there's apparently an error for marshalbyref objects where they get removed after a while without this
{
return null;
}
public Assembly GetAssembly(byte[] assembly)
{
try
{
return Assembly.Load(assembly);
}
catch (Exception e)
{
Console.WriteLine(e);
}
return null;
}
public Assembly GetAssembly(string filepath)
{
try
{
return Assembly.LoadFrom(filepath);
}
catch (Exception e)
{
Console.WriteLine(e);
}
return null;
}
}
public static Assembly LoadAssembly(string modName, BinBuffer bb)
{
string assembly = pathDirTemp+"/"+modName+".dll";
File.WriteAllBytes(assembly, bb.ReadBytes(bb.BytesLeft()));
RemoteDomain loader = (RemoteDomain)modsDomain.CreateInstanceAndUnwrap(typeof(RemoteDomain).Assembly.FullName, typeof(RemoteDomain).FullName);
return loader.GetAssembly(assembly);
}
尽可能具体:有没有办法让一个可卸载的 AppDomain 加载不在应用程序基本文件夹中的程序集?