Created
August 7, 2012 21:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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