.NET:无法施展对象的接口它实现 [英] .NET: Unable to cast object to interface it implements

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

问题描述

我有一个类(TabControlH60)从基类(用户控件)都继承并实现一个接口(IFrameworkClient)。我实例使用.NET Activator类的对象。使用返回的实例,我可以转换为用户控件的基类,而不是接口。我得到的唯一的例外是code snipet下方。我如何转换为界面?

  obj对象= Activator.CreateInstance(OBJTYPE);
键入[] =接口obj.GetType()GetInterfaces()。 //包含IFrameworkClientm_Client =(用户控件)目标文件; //基类作品投
IFrameworkClient FC =(IFrameworkClient)目标文件; //接口转换失败//注:(IFrameworkClient)OBJ投工作正常,在调试器监视窗口。
{无法投型FPG.H60​​.AFF.TabControlH60对象键入
    FPG.AFF.Interfaces.IFrameworkClient'。}


解决方案

我的帽子和我的一个库中相同的问题提供插件-Functionality ......我终于工作...

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

的Plugin.dll引用主组件(为了能够将它扩展)和Library.dll用插件-FUNC。 - 它是二进制到了一个目录./Plugins相对于主组件

主要组件还引用的插件,FUNC。装配,以使用插件管理中写道。这种插件管理通过反射得到的路径,并加载所有* .dll文件,以分析是否有IPlugin-interface(它来自Library.dll太)。

每次我叫插件管理加载插件,它可以不投他们IPlugin虽然他们付诸实施。

我几乎疯了 - 但后来我发现了问题的全部。通过编写插件,不仅传出了Plugin.dll,但Library.dll写入./Plugins目录。意外地加载Library.dll我的插件管理每次我现在有两种类型的IPlugin的 - 一个在一个从主组件,那就是通过我的插件管理加载到实际使用的Library.dll - 和那些不兼容!

注意 - 如果你只是不加载./Plugins/Library.dll你仍然遇到这个问题 - 因为如果你加载Plugin.dll,它引用Library.dll,那么它​​只是使用了一个在同一目录... ... TILT!我的插件管理现在只是删除Library.dll的地方找到它。

线索是:请确保您不会在不同环境下访问两个组件

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...

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).

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.

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).

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

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!

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天全站免登陆