IsAssignableFrom()时,它应该返回true返回falseIsAssignableFrom、true、false

2023-09-03 00:47:49 作者:毁了心葬了爱

我工作的一个插件系统加载.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.

我检查这个与code类似以下内容:

I am checking this with code similar to the following:

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

由于某些原因IsAssignableFrom()保持返回false时,应该返回true。我曾尝试更换 T 通过明确地给它应该通过一个类型,它工作正常,但由于某种原因它不工作与从返回的类型在加载的程序集。为了使事情变得陌生,在code正常工作,在我的同事的机器,但不是我的。

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,目前的程序集引用的组件之间不匹配,而这是由组装containg类型引用的组件你遍历。

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

要看到的不匹配。