Search code examples
c#.netreflectiontype-systemssystem.type

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


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?


Solution

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