Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 753
sh0gged Avatar asked Sep 16 '09 15:09

sh0gged


2 Answers

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

like image 122
alex Avatar answered Oct 06 '22 01:10

alex


You are probably looking for Type.IsAssignableFrom.

like image 30
Aistina Avatar answered Oct 06 '22 01:10

Aistina