1

我有以下程序

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)

在我的 Installer 扩展类中定义:

[RunInstaller(true)]
public class SpheresServiceInstaller : Installer

当我尝试将展开的对象转换为所需类型时出现异常:

interface = (IMyInterface)myobject ;

异常详情:

  • 类型:System.InvalidCastException
  • 消息:无法将透明代理转换为类型“IMyInterface”。

我想了解为什么该程序一直有效,但在这种特定情况下。

细节

  • 我一步一步地遵循对象创建过程,一切似乎都很好,该对象是由 Activator.CreateInstance 过程很好地创建的。

  • Activator.CreateInstance 使用的程序集已经存在于文件系统中。

  • 具体程序集'assemblyName on the source code)是安装程序刚刚创建的窗口服务。

4

1 回答 1

0

我按照这篇文章中提出的链接解决了这个问题

堆栈溢出:appdomain-createinstancefromandunwrap-unable-to-cast-transparent-proxy

为我们提供解决方案代码的链接是

west-wind.com:跨 appdomain 的程序集加载

这确实是一件基本的事情,我陷入了由外部应用程序加载程序集的情况(在我的特定情况下:wow64 安装程序应用程序)。

应用程序不知道在哪里可以找到依赖于您正在加载的主程序集的程序集,因此您必须为当前应用程序域(在我的特定情况下:wow64 安装程序应用程序)编写自定义程序集解析器,以便提供里面有必要的加载信息。

前往 west-winf 链接获取代码,它完美运行

于 2010-06-25T12:27:34.950 回答