Does Type Implement an Interface?

I’ve wasted far too much time on this issue, so I’ll make an entry for future frustrated Google searchers. You’re welcome.

If you need to see if a particular Type object implements a particular interface, it’s clear that the method IsAssignableFrom is the proper choice. But the damn thing wouldn’t work. is certainly what I was looking for. Then I found Scott Hanselman’s entry on the subject and it became obvious that I was reversing the members.

Assembly library = Assembly.LoadFrom(pathToLibrary);
Type[] allTypes = library.GetTypes();
foreach (Type type in allTypes)
{
if (typeof(IInterfaceToImplement).IsAssignableFrom(type))
{
doYourThing(type);
}
}

Previously, I was doing if (type.IsAssignableFrom(typeof(IInterfaceToImplement)) and it just wasn’t doing it’s thing.

Advertisements

%d bloggers like this: