Alex Rivera | Logout

How do you determine whether or not a given Type (System.Type) inherits from a specific base class (in .Net)?

Asked 2008-09-24T19:21:39.747
25

This is likely going to be an easy answer and I'm just missing something, but here goes...If I have a Type, (that is, an actual System.Type...not an instance) how do I tell if it inherits from another specific base type?

Edit
Report

1 Answer

22

EDIT: Note that the above solution will fail if the base type you are looking for is an interface. The following solution will work for any type of inheritance, be it class or interface.

// Returns true if "type" inherits from "baseType"
public static bool Inherits(Type type, Type baseType) {
    return baseType.IsAssignableFrom(type)
}

(Semi)Helpful extract from the MSDN article:

true if [the argument] and the current Type represent the same type, or if the current Type is in the inheritance hierarchy of [the argument], or if the current Type is an interface that [the argument] implements, or if [the argument] is a generic type parameter and the current Type represents one of the constraints of [the argument]. false if none of these conditions are true, or if [the argument] is a null reference (Nothing in Visual Basic).

answered 2008-09-24T19:24:05.357

Your Answer