Skip to content

Instantly share code, notes, and snippets.

@pedroreys
Created February 18, 2013 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedroreys/4981597 to your computer and use it in GitHub Desktop.
Save pedroreys/4981597 to your computer and use it in GitHub Desktop.
public static class TypeExtensions
{
public static bool IsAssignableToGenericType(this Type givenType, Type genericType)
{
var interfaceTypes = givenType.GetInterfaces();
if (interfaceTypes.Any(it => it.IsGenericType && it.GetGenericTypeDefinition() == genericType))
{
return true;
}
if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
{
return true;
}
var baseType = givenType.BaseType;
if (baseType == null)
{
return false;
}
return IsAssignableToGenericType(baseType, genericType);
}
public static bool IsEnumeration(this Type givenType)
{
return givenType.IsAssignableToGenericType(typeof(Enumeration<>));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment