Best way to check if System.Type is a descendant of a given class

sh0gged picture sh0gged · Sep 16, 2009 · Viewed 9k times · Source

Consider the following code:

public class A 
{
}  

public class B : A 
{
}  

public class C : B 
{
}  

class D  
{  
    public static bool IsDescendantOf(this System.Type thisType, System.Type thatType)  
    {  
        /// ??? 
    } 

    void Main()
    {
        A cValue = new C();
        C.GetType().IsDescendantOf(cValue.GetType());
    }
}

What is the best way to implement IsDescendantOf?

Answer

alex picture alex · Sep 16, 2009

Type.IsSubclassOf() Determines whether the class represented by the current Type derives from the class represented by the specified Type.