This is an interesting problem I had recently — and, even more interestingly, I couldn’t find a built-in solution for this. .NET is pretty exhaustive so I found this pretty suprising.
I found a little info on Scott Hanselman’s blog about this and was able to come up with a simple solution to the problem.
public static class TypeExtensions
{
public static bool IsImplementationOf(this Type baseType, Type interfaceType)
{
return baseType.GetInterfaces().Any(interfaceType.Equals);
}
}
Now I’m not 100% sure what the performance implications are here, but at the very least it’s clean. Extension methods are a god send and, thankfully, IntelliSense calls them out pretty well so you’re always aware of what you’re calling!
Advertisement