Check If a Class Is Derived from a Generic Class

Check if a class is derived from a generic class

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;
}

Determine if object is an instance of a generic base class, any generic type

The problem is that DrevidedC1 is not a sublcass of Class1<T>, it's a subclass of Class1<int>. Make sure you understand this subtle diference; Class1<T> is a open type (T can be anything, it hasn't been set) while DerivedC1 extends a closed type Class1<int> (it's not open in T anymore, T is set to int and only int). So when you do the following:

 typeof(DerivedC1).IsSubclassOf(typeof(Class1<>))

The answer is evidently false.

What you need to do is check if the generic type definition of DerivedC1's base type (think of it as the corresponding open generic type of Class1<int>) equals Class1<T> which it clearly does.

The correct code is therefore:

typeof(DerivedC1).BaseType.GetGenericTypeDefinition() == typeof(Class1<>));

Or better yet, as Matías Fidemraizer states in his answer:

typeof(DerivedC1).BaseType.GetGenericTypeDefinition().IsAssignableFrom(typeof(Class1<>)));

Check if type is derived from abstract generic class

You must to check with your own hands all base types:

private static bool IsSubclassOfRawGeneric(Type baseType, Type derivedType) {
while (derivedType != null && derivedType != typeof(object)) {
var currentType = derivedType.IsGenericType ? derivedType.GetGenericTypeDefinition() : derivedType;
if (baseType == currentType) {
return true;
}

derivedType = derivedType.BaseType;
}
return false;
}

And then you can use it like this:

    var validatorType = typeof(AbstractValidator<>);
var subTypes = validatorType.Assembly
.GetTypes()
.Where(t => IsSubclassOfRawGeneric(validatorType, t));

Ideone: R7Q88Z

check if instance inherited from generic or non generic version of a class in C#

A instance of class b is not derived from a<T>, but from a<int>, the two are different types.

var type = inst.GetType();
var result = false;

while (type != typeof(object))
{
type = type.BaseType;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(a<>))
{
result = true;
break;
}
}

//check result

Find all derived types of generic class


var result = System.Reflection.Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.BaseType != null && t.BaseType.IsGenericType &&
t.BaseType.GetGenericTypeDefinition() == typeof(GenericClass<>));

How to determine if an object inherits from an abstract generic class

Recursively

Type type2 = type; // type is your type, like typeof(ClassA)

while (type2 != null)
{
if (type2.IsGenericType &&
type2.GetGenericTypeDefinition() == typeof(SuperClass<>))
{
// found
}

type2 = type2.BaseType;
}

Note that this won't work if you are looking for an interface!

How to detect a subclass of a generic abstract class?

You need to check if your testClass is a subclass of TestBaseClass<int> instead of TestBaseClass<> since you could inherit from a different generic 'version' of this class.

So using the following code isSubclass returns true:

var testClass = typeof(TestClass);
var isSubclass = testClass.IsSubclassOf(typeof(TestBaseClass<int>));

If it is the case that you want to check if the class inherits TestBaseClass regardless of the used generic type you can try the following:

var testClass = typeof(TestClass);
if (testClass.BaseType.IsGenericType &&
testClass.BaseType.GetGenericTypeDefinition() == typeof(TestBaseClass<>))
{
//testClass inherits from TestBaseClass<>
}


Related Topics



Leave a reply



Submit