IsAssignableFrom() returns false when it should return true

Some other answers have mentioned the lack of clarity in the name of the IsAssignableFrom method. I agree, and as a result was using it in the wrong way.

Try a little experimenting with reversing the objects in your code and see if it works. For example:

Replace:

if (typeof(IPlugin).IsAssignableFrom(t))

with:

if (t.IsAssignableFrom(typeof(IPlugin)))

By doing this I not only got it to work, but began to understand what this method actually does.


I had same issue when interface was defined in a separate assembly to implementing type. Iterating and loading assemblies from root folder that contained dlls with classes AND dll with interface resulted in type mismatch as mentioned above.

One solution was to change LoadFrom() to LoadFile() The LoadFrom method has some disadvantages and that is one of them:

If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified.

Another way to overcome this is to place all dlls with types implementing interface into separate folder and not to copy referenced assembly (CopyLocal = False) so Assembly.LoadFrom will not load dll containing interface in memory.


Sometimes it's an issue with the dynamic assembly referencing another assembly.

One simple thing to do it to disable local copy on the assembly (in visual studio right click the reference and set copy local to false). This should make it easier to nail down the directory where the assembly lives.

You can also implement an assembly resolver in-case .NET doesn't know how to initalize the type.

        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler([Resolve Function]);

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.

I suggest you print:

typeof (IPlugin).Module.FullyQualifiedName

and

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

To see where the mismatch is.