Skip to content

Instantly share code, notes, and snippets.

@kcargile
Created August 7, 2012 21:02
Show Gist options
  • Save kcargile/3289331 to your computer and use it in GitHub Desktop.
Save kcargile/3289331 to your computer and use it in GitHub Desktop.
.NET extension method to determine if a type's superclass is of non-specific generic type.
using System;
namespace Extensions
{
public static class TypeExtensions
{
public static bool IsSubclassOfRawGeneric(this Type type, Type generic)
{
if (null == generic)
{
throw new ArgumentNullException("generic");
}
if (type.IsInterface || type.IsValueType)
{
return false;
}
while (type != typeof(object))
{
Type currentType = type.IsGenericType ? type.GetGenericTypeDefinition() : type;
if (currentType != null && currentType.IsGenericType && generic.GetGenericTypeDefinition() == currentType.GetGenericTypeDefinition())
{
return true;
}
type = type.BaseType;
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment