Every once in a while, you will have to do is checks using reflection.
Basically there are two methods:
- Type.IsSubclassOf checks strict inheritance checking
- Type.IsAssignableFrom is equivalent to the
is
operator so it also works for interfaces.
Thanks to Ani and Werner Beroux for explaining the difference in more detail on StackOverflow, I added some extra links:
To check for assignability, you can use the Type.IsAssignableFrom method:
typeof(SomeType).IsAssignableFrom(typeof(Derived))
This will work as you expect for
but not when you are looking for ‘assignability’ across explicit / implicit conversion operators.
To check for strict inheritance, you can use Type.IsSubclassOf:
typeof(Derived).IsSubclassOf(typeof(SomeType))
–jeroen
via: c# – How to check if a class inherits another class without instantiating it? – Stack Overflow.