bernhardrusch bernhardrusch - 1 year ago 72
C# Question

Check if a class is derived from a generic class

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
object is derived from


does not work.

Answer Source

Try this code

static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {
    while (toCheck != null && toCheck != typeof(object)) {
        var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
        if (generic == cur) {
            return true;
        toCheck = toCheck.BaseType;
    return false;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download