.NET:无法将对象转换为它实现的接口 [英] .NET: Unable to cast object to interface it implements

查看:16
本文介绍了.NET:无法将对象转换为它实现的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 (TabControlH60),它既继承自基类 (UserControl),又实现了接口 (IFrameworkClient).我使用 .NET Activator 类实例化对象.使用返回的实例,我可以转换为 UserControl 基类,但不能转换为接口.我得到的例外是在代码片段下方.如何投射到界面?

I have a class (TabControlH60) that both inherits from a base class (UserControl) and implements an interface (IFrameworkClient). I instantiate the object using the .NET Activator class. With the returned instance, I can cast to the UserControl base class, but not to the interface. The exception I get is below the code snipet. How do I cast to the interface?

object obj = Activator.CreateInstance(objType);
Type[] interfaces = obj.GetType().GetInterfaces(); // contains IFrameworkClient

m_Client = (UserControl)obj;                 // base class cast works
IFrameworkClient fc = (IFrameworkClient)obj; // interface cast fails

// Note: The (IFrameworkClient)obj cast works fine in the debugger Watch window.
{"Unable to cast object of type 'FPG.H60.AFF.TabControlH60' to type 
    'FPG.AFF.Interfaces.IFrameworkClient'."}

推荐答案

我的一个提供插件"功能的库遇到了同样的问题......我终于让它工作了......

I hat the same problems with a library of mine providing "plugin"-functionality... I got it finally working...

这是我的问题:我有一个使用插件的主程序集,一个带有插件的程序集(Plugin.dll)和(重要的)另一个提供插件功能的程序集(Library.dll).

Here was my problem: I had one main assembly using plugins, one assembly with the plugin (Plugin.dll) AND (important) another assembly providing the plugin-functionality (Library.dll).

Plugin.dll 引用了主程序集(为了能够扩展它)和带有 plugin-func 的 Library.dll.- 它的二进制文件位于相对于主程序集的目录./Plugins".

The Plugin.dll referenced the main assembly (in order to be able to extend it) and the Library.dll with the plugin-func. - it's binaries got to a directory "./Plugins" relative to the main assembly.

主程序集也引用了 plugin-func.程序集是为了使用PluginManager"而写的.这个PluginManager"获取路径并通过反射加载所有*.dll文件,以分析是否有IPlugin"接口(也来自Library.dll).

The main assembly also referenced the plugin-func. assembly in order to use the "PluginManager" is wrote. This "PluginManager" gets a path and loads all *.dll files via reflection in order to analyze if there is a "IPlugin"-interface (which comes from Library.dll too).

每次我调用 PluginManager 加载插件时,它都无法将它们转换为IPlugin",尽管他们实现了它.

Everytime I called the PluginManager to load the plugins it could not cast them to "IPlugin" although they implemented it.

我差点生气 - 但后来我发现了整个问题.通过编译插件,不仅Plugin.dll"而且Library.dll"被写入./Plugins"目录.由于每次使用 PluginManager 意外加载Library.dll",我现在有两种类型的IPlugin" - 一种在主程序集中使用的实际Library.dll"中,另一种通过我的 PluginManager 加载 - 和那些不兼容!

I nearly got mad - but then I found out the whole problem. By compiling the plugin there was not only the "Plugin.dll" but the "Library.dll" written to the "./Plugins" directory. By accidentally loading the "Library.dll" every time with my PluginManager I now had two types of "IPlugin" - one in the actual "Library.dll" that is used from the main assembly and one that was loaded through my PluginManager - and those were incompatible!

注意-如果您只是不加载./Plugins/Library.dll",您仍然会遇到问题-因为如果您加载引用Library.dll"的Plugin.dll",那么它只会使用相同的目录...倾斜...!!我的 PluginManager 现在只是删除它找到的Library.dll".

Attention - if you just do not load "./Plugins/Library.dll" you nevertheless encounter the problem - because if you load "Plugin.dll" which references "Library.dll" then it just uses the one in the same directory... TILT...!! My PluginManager now just deletes "Library.dll" where it find it.

提示是:请确保不要在不同的上下文中访问两个程序集!

The clue is: Be sure that you do not access two assemblies in different contexts!

这篇关于.NET:无法将对象转换为它实现的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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