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?

Brandon Montgomery
  • 6,924
  • 3
  • 48
  • 71
Kilhoffer
  • 32,375
  • 22
  • 97
  • 124
  • 2
    Note that the [Type.IsSubclassOf](http://msdn.microsoft.com/en-us/library/system.type.issubclassof.aspx) method does not work for generic types! [**Take a look at this article**](http://www.pvladov.com/2012/05/get-all-derived-types-of-class.html) for an implementation of the IsSubclassOf method that works for generics as well. – Pavel Vladov Jun 08 '12 at 10:13

3 Answers3

32

Use the IsSubclassOf method of the System.Type class.

chakrit
  • 61,017
  • 25
  • 133
  • 162
23

One thing to clarify between Type.IsSubTypeOf() and Type.IsAssignableFrom():

  • IsSubType() will return true only if the given type is derived from the specified type. It will return false if the given type IS the specified type.

  • IsAssignableFrom() will return true if the given type is either the specified type or derived from the specified type.

So if you are using these to compare BaseClass and DerivedClass (which inherits from BaseClass) then:

BaseClassInstance.GetType.IsSubTypeOf(GetType(BaseClass)) = FALSE
BaseClassInstance.GetType.IsAssignableFrom(GetType(BaseClass)) = TRUE

DerivedClassInstance.GetType.IsSubTypeOf(GetType(BaseClass)) = TRUE
DerivedClassInstance.GetType.IsAssignableFrom(GetType(BaseClass)) = TRUE
Yahoo Serious
  • 3,728
  • 1
  • 33
  • 37
STW
  • 44,917
  • 17
  • 105
  • 161
  • 9
    Actually, DerivedClassInstance.GetType.IsAssignableFrom(GetType(BaseClass)) will return FALSE. You need to apply it the other way round: typeof(BaseClass).IsAssignableFrom(typeof(DerivedClass)). In other words, you can't do the following: DerivedClass c = new BaseClass(); Which is what IsAssignableFrom is testing – jspaey Jun 04 '12 at 12:23
  • If you have just the type in your hand. Such as `propertyInfo.PropertyType` how can you determine if an interface implemented from this type? IsAssignableFrom just gives compatibility between them. To excite a more clear example;I could not found if a type is really and exactly IList over a type List. It could be another thing driven from IList. This should be Polymorphic issue. – Davut Gürbüz May 22 '13 at 09:09
  • @DavutGürbüz interfaces are a different issue from classes, but your scenario sounds like a code-smell. It shouldn't matter where the interface implementation is declared--if your class implements an interface, either directly or indirectly, then it should be interchangable with all other implementations. Otherwise you're violating Liskov's substitution principle. – STW May 22 '13 at 13:42
  • 1
    Yes that is really what I meant. Yeah we may think this as code-smell, it does not matter interface or a subclass what I want to say depending on Liskov's rule you can't get much from `IsAssignableFrom` method. But if you were have an instance you could compare by `(instance.GetType() == typeOf(IList))` , to determine if it is exactly the type we are looking for.(works differently from `is` or `as` operators as you know). We have no chance when we have just the type for exact comparison. – Davut Gürbüz May 22 '13 at 13:57
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).

Chris Marasti-Georg
  • 34,091
  • 15
  • 92
  • 137