IsAssignableFrom() 在应该返回 true 时返回 false [英] IsAssignableFrom() returns false when it should return true

查看:38
本文介绍了IsAssignableFrom() 在应该返回 true 时返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个插件系统,该系统加载包含在指定文件夹中的 .dll.然后我使用反射来加载程序集,遍历它们包含的类型并识别任何实现我的 IPlugin 接口的类型.

I am working on a plugin system that loads .dll's contained in a specified folder. I am then using reflection to load the assemblies, iterate through the types they contain and identify any that implement my IPlugin interface.

我正在使用类似于以下的代码进行检查:

I am checking this with code similar to the following:

foreach(Type t in myTypes )
{
    if( typeof(IPlugin).IsAssignableFrom(t) )
    {
       ...
    }
}

出于某种原因,IsAssignableFrom() 在应该返回 true 时却一直返回 false.我已经尝试通过显式地给它一个应该通过的类型来替换 t,它工作正常,但由于某种原因,它不适用于从加载的程序集返回的类型.更奇怪的是,代码在我同事的机器上运行良好,但在我的机器上运行良好.

For some reason IsAssignableFrom() keeps returning false when it should be returning true. I have tried replacing the t by explicitly giving it a type that should pass, and it works fine, but for some reason it isn't working with the types that are returned from the loaded assembly. To make things stranger, the code works fine on my co-worker's machine but not on mine.

有没有人知道可能导致这种行为的任何事情?

Does anyone know of anything that might cause this sort of behavior?

谢谢

推荐答案

当包含当前程序集引用的 IPlugin 类型的程序集与包含这些类型的程序集引用的程序集不匹配时,通常会发生这种情况你正在迭代.

That typically happens when there's a mismatch between the assembly which contains the type IPlugin that the current assembly references, and the assembly which is referenced by the assembly containg the types you're iterating over.

我建议你打印:

typeof (IPlugin).Module.FullyQualifiedName

foreach (var type in t.GetInterfaces ()) 
{    
    Console.WriteLine (type.Module.FullyQualifiedName)
}

查看不匹配的位置.

这篇关于IsAssignableFrom() 在应该返回 true 时返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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