Let\'s say I have a type, MyType. I want to do the following:
Using Anton Tykhyy's proposal, here is a small Extension Method to check if some type implements a generic interface with one given generic type parameters:
public static class ExtensionMethods
{
///
/// Checks if a type has a generic interface.
/// For example
/// mytype.HasGenericInterface(typeof(IList<>), typeof(int))
/// will return TRUE if mytype implements IList
///
public static bool HasGenericInterface(this Type type, Type interf, Type typeparameter)
{
foreach (Type i in type.GetInterfaces())
if (i.IsGenericType && i.GetGenericTypeDefinition() == interf)
if (i.GetGenericArguments()[0] == typeparameter)
return true;
return false;
}
}