我有以下程序
private static IMyInterface OpenInstance(
string assemblyPath,
string classType,
string assemblyName,
out AppDomain domainInstall)
{
IMyInterface interface = null;
AppDomainSetup domaininfo = new AppDomainSetup();
domaininfo.ApplicationBase = assemblyPath;
domainInstall = AppDomain.CreateDomain("PathInstall", null, domaininfo);
ObjectHandle handleService = null;
try
{
handleService = Activator.CreateInstance(
domainInstall,
assemblyName,
classType,
true,
System.Reflection.BindingFlags.CreateInstance,
null,
new Object[] { assemblyName},
System.Globalization.CultureInfo.CurrentCulture,
null, null);
Object myobject = handleService.Unwrap();
interface = (IMyInterface )myobject ;
}
catch (Exception ex)
{
...
}
return interface ;
}
此过程工作得很好,但在安装自定义操作期间调用它时。
换句话说,如果我在自己的 Install(...) 覆盖中调用 OpenInstance(...) 过程:
public override void Install(IDictionary stateServer)
在我的安装程序扩展类中定义:
[RunInstaller(true)]
public class SpheresServiceInstaller : Installer
当我尝试将未包装的对象转换为所需的类型时出现异常:
interface = (IMyInterface)myobject ;
异常详细信息:
-
Type: 系统.InvalidCastException
-
Message:无法将透明代理强制转换为类型“IMyInterface”。
我想了解为什么该程序在这种特定情况下有效。
Details
我一步步遵循对象创建过程,一切看起来都很好,该对象是由 Activator.CreateInstance 过程很好地创建的。
Activator.CreateInstance 使用的程序集已存在于文件系统中。
源代码中的特定程序集“AssemblyName”是安装过程刚刚创建的窗口服务。