一个Activator.CreateInstance对象的安装过程中InvalidCastException的 [英] InvalidCastException of a Activator.CreateInstance object during an installation procedure
问题描述
我有以下程序
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 ;
}
此过程适用以往任何时候都很好,但是当它安装自定义操作过程中被调用。
This procedure works ever fine, but when it is called during an installation custom action.
在换句话说,如果我叫在我自己安装(...)重写我的OpenInstance(...)方法:
In other words if i call my OpenInstance(...) procedure inside my own Install(...) override:
public override void Install(IDictionary stateServer)
定义在我的安装程序扩展类:
Defined in my Installer extended class:
[RunInstaller(true)]
public class SpheresServiceInstaller : Installer
我有一个例外,当我尝试投我解开对象所需的类型:
I got an exception when i try to cast my unwrapped object to the desired type:
interface = (IMyInterface)myobject ;
异常详细信息:
- 的键入的:System.InvalidCastException
- 的消息的:无法施展透明代理键入IMyInterface的
- Type: System.InvalidCastException
- Message: Unable to cast transparent proxy to type 'IMyInterface'.
我想明白为什么程序的工作不断,但在这种特殊情况下。
I would like to understand why the procedure works ever but in this specific case.
详细信息
-
我步步紧跟对象的创建过程,一切似乎很动听,对象是由Activator.CreateInstance程序以及创建。
I followed step by step the object creation procedure and everything seems fine, the object is well created by the Activator.CreateInstance procedure.
的组件,其中所使用的Activator.CreateInstance,已经存在的文件系统上。
The assembly, which is used by the Activator.CreateInstance, already exists on the file system.
在源$ C $ C的具体程序集的AssemblyName)是已经仅仅通过安装程序创建的窗口服务。
The specific assembly 'assemblyName on the source code) is a window service that has been just created by the installation procedure.
推荐答案
我解决了以下这个帖子提出该链接的问题
I solved the problem following the link proposed at this post
<一个href="http://stackoverflow.com/questions/1437831/appdomain-createinstancefromandunwrap-unable-to-cast-transparent-proxy">stack溢出:应用程序域-createinstancefromandunwrap,无法对铸透明代理
这为我们提供了解决方案,code的链路
The link that gives us the solution code is
west-wind.com:大会跨越AppDomain中加载
这实在是一个基本的东西,我是在地方自然落下,其中一个组件是由外部应用程序(在我的具体情况:在WOW64安装程序)加载的情况。
It is really a basic thing, i was falled in the case where an assembly is loaded by an external application (in my specific case: the wow64 installer application).
应用程序不知道在哪里可以找到依赖从正在加载的主要部件,所以你必须编写自定义程序集解析器为当前应用程序域的组件(在我的具体情况:在WOW64安装应用程序),以给予必要的负载信息在里面。
The application does not know where to find the assemblies that depend from the main assembly you are loading, so you have to write a custom assembly resolver for the current application domain (in my specific case: the wow64 installer application) in order to give the necessary loading information inside it.
头到西winf链接可以获得$ C $ C,它完美的作品
Head to the west-winf link to get the code, it works perfectly
这篇关于一个Activator.CreateInstance对象的安装过程中InvalidCastException的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!