AlanTsai
5/9/2016 - 4:19 AM

Check up the inheritance chanin if a class inherit a generic type From http://stackoverflow.com/questions/457676/check-if-a-class-is-derived

Check up the inheritance chanin if a class inherit a generic type From http://stackoverflow.com/questions/457676/check-if-a-class-is-derived-from-a-generic-class #csharp

public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic) {
    while (toCheck != null && toCheck != typeof(object)) {
        var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
        if (generic == cur) {
            return true;
        }
        toCheck = toCheck.BaseType;
    }
    return false;
}

I have a generic class in my project with derived classes.

public class GenericClass<T> : GenericInterface<T>
{
}
public class Test : GenericClass<SomeType>
{
}

Is there any way to find out if a Type object is derived from GenericClass?

t.IsSubclassOf(typeof(GenericClass<>))