无法投射物体 [英] Unable to cast object

查看:68
本文介绍了无法投射物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个程序集(Windows服务)中有一个C ++ / CLI接口IPlugin,它由一个类CPlugin继承并在另一个程序集(dll)中实现。我想要能够在运行时动态创建这个类的实例,所以我

使用


加载dll程序集汇编^ assembly = Assembly :: LoadFrom(目录+ assemblyName +" .dll");


然后使用
创建一个CPlugin实例

Object ^ obj =(assembly-> CreateInstance(assemblyName +" .C" + assemblyName));


其中assemblyName是PlugIn。这样就可以很快地创建一个对象的实例。

。但是,当我尝试执行以下操作时,它会抛出InvalidCastException


IPlugin ^ iPI = safe_cast< IPlugin ^>(obj);


我也尝试过dynamic_cast,返回一个未定义的对象。


所以问题是为什么我不能在继承它时将CPlugin转换为IPlugin

publically ?


谢谢


Colin

I have a C++/CLI interface IPlugin in one assembly (a windows service) that
is inherited and implemented in another assembly (dll) by a class CPlugin. I
want to be able to create instance of this class dynamically at runtime, so I
load the dll assembly using

Assembly^ assembly = Assembly::LoadFrom(directory + assemblyName + ".dll");

then create an instance of CPlugin using

Object^ obj = (assembly->CreateInstance(assemblyName+".C"+assemblyName)) ;

Where assemblyName is PlugIn. This creates an instance of an object quite
happily. But when I try to do the following it throws an InvalidCastException

IPlugin^ iPI = safe_cast<IPlugin^>(obj);

I have also tried dynamic_cast which returns a undefined object.

So the question is why can''t I cast CPlugin to IPlugin when it inherits it
publically?

Thanks

Colin

推荐答案

>我在一个程序集(Windows服务)中有一个C ++ / CLI接口IPlugin,
>I have a C++/CLI interface IPlugin in one assembly (a windows service) that
由CPlugin类继承并在另一个程序集(dll)中实现。
我想要能够在运行时动态创建此类的实例,
所以我使用
装配dll程序集
装配^ assembly = Assembly :: LoadFrom(目录+ assemblyName +
" .dll");

然后使用

对象^ obj =(assembly-> CreateInstance(assemblyName +" .C&quo)创建一个CPlugin实例t; + assemblyName));

其中assemblyName是PlugIn。这非常愉快地创建了一个对象的实例。但是,当我尝试执行以下操作时,它会抛出一个
InvalidCastException

IPlugin ^ iPI = safe_cast< IPlugin ^>(obj);

我也尝试过dynamic_cast返回一个未定义的对象。

所以问题是为什么我不能在继承它时将CPlugin转换为IPlugin
公开?
is inherited and implemented in another assembly (dll) by a class CPlugin.
I
want to be able to create instance of this class dynamically at runtime,
so I
load the dll assembly using

Assembly^ assembly = Assembly::LoadFrom(directory + assemblyName +
".dll");

then create an instance of CPlugin using

Object^ obj = (assembly->CreateInstance(assemblyName+".C"+assemblyName)) ;

Where assemblyName is PlugIn. This creates an instance of an object quite
happily. But when I try to do the following it throws an
InvalidCastException

IPlugin^ iPI = safe_cast<IPlugin^>(obj);

I have also tried dynamic_cast which returns a undefined object.

So the question is why can''t I cast CPlugin to IPlugin when it inherits it
publically?



您是否在服务程序集和插件程序集中声明了IPlugin?



Did you declare IPlugin both in service assembly and in plugin assembly?


您是否在服务程序集和服务程序集中声明了IPlugin在插件程序集中?
Did you declare IPlugin both in service assembly and in plugin assembly?



不,我在服务中声明它然后在插件中引用它

汇编。


Colin


No, I declared it in the service and then referenced it in the plugin
assembly.

Colin


>>你在服务程序集和插件程序集中都声明了IPlugin吗?
>> Did you declare IPlugin both in service assembly and in plugin assembly?


不,我在服务中声明了它,然后引用它插件
汇编。

Colin


No, I declared it in the service and then referenced it in the plugin
assembly.

Colin




您可以在调试器中检查是否有两个服务实例

程序集已加载?一旦进入BizTalk,我已经看到一些程序集是从GAC和bin文件夹加载两次
。尽管

组合是相同的,但运行时认为它们是不同的。结果

在一个程序集中创建的类无法转换为相应的类

来自第二个程序集。

-

Vladimir Nesterovsky



Can you check in the debugger whether there are two instances of the service
assembly are loaded? Once in the BizTalk I''ve seen that some assembly was
loaded twice from GAC and from a bin folder. In spite of the fact that
assembies was identical runtime considered them as different. As result
classes created in one assembly could not be casted to corresponded classes
from second assembly.
--
Vladimir Nesterovsky


这篇关于无法投射物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆