我正在尝试将程序集加载到应用程序域中。我可以成功将程序集加载到当前域中,但我需要将其加载到应用程序域中,以便我可以卸载它。代码如下:
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\XXX\\XXX.dll";
AppDomain xxxDomain = AppDomain.CreateDomain("XXX Domain");
Type type = typeof(ProxyDomain);
ProxyDomain xxxInstance = (ProxyDomain)xxxDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
Assembly xxxAssembly = xxxInstance.GetAssembly(path);
但是,当我调用Load时,我收到以下错误:
无法加载文件或程序集XXX,版本= 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其中一个依赖项。系统找不到指定的文件。
ProxyDomain类如下:
class ProxyDomain : MarshalByRefObject
{
public Assembly GetAssembly(string path)
{
try
{
return Assembly.LoadFrom(path);
}
catch (Exception ex)
{
throw new InvalidOperationException("Failed to load assembly from path: " + path, ex);
}
}
}
提前谢谢。