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.

About this Entry

This page contains a single entry by bbrown published on December 20, 2006 12:07 PM.

Limerick Challenge was the previous entry in this blog.

Can't Pull the Rug Out From Under Yourself is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Feedback to