Created
January 7, 2019 10:34
-
-
Save niikoo/624ece1e53f3effc09a8a635061e3cc3 to your computer and use it in GitHub Desktop.
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
namespace Extensions { | |
public static class GenericExtensions | |
{ | |
/// <summary> | |
/// Check if class inherits a raw generic type, i.e. IEnumerable<> | |
/// </summary> | |
/// <param name="generic"></param> | |
/// <param name="toCheck"></param> | |
/// <returns></returns> | |
public static bool IsSubClassOfRawGeneric(this Type toCheck, Type generic, bool checkInterfaces = true) | |
{ | |
while (toCheck != null && toCheck != typeof(object)) | |
{ | |
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck; | |
if (generic == cur) | |
return true; | |
if (checkInterfaces && toCheck.GetInterfaces().Any(iface => iface.Name == generic.Name)) | |
return true; | |
toCheck = toCheck.BaseType; | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment