Mono Class Library: System.Type Overview | Members

System.Type.IsSubclassOf Method

Determines whether the current Type derives from the specified Type .

[System.Runtime.InteropServices.ComVisible(true)]
public virtual bool IsSubclassOf (Type c)

Parameters

c
The Type to compare with the current Type.

Returns

true if c and the current Type represent classes, and the class represented by the current Type derives from the class represented by c; otherwise false. Returns false if c and the current Type represent the same class.

Remarks

Interfaces are not considered.

If the current instance represents an unassigned type parameter of a generic type or method, it derives from its class constraint, or from object if it has no class constraint.

Example

The following example demonstrates the Type.IsSubclassOf(Type) method.

C# Example

using System;
public interface IFoo { }
public interface IBar:IFoo{}
public class MyClass : IFoo {}
public class MyDerivedClass : MyClass {}
class IsSubclassTest {
 public static void Main() {
 Type ifooType = typeof(IFoo);
 Type ibarType = typeof(IBar);
 MyClass mc = new MyClass();
 Type mcType = mc.GetType();
 MyClass mdc = new MyDerivedClass();
 Type mdcType = mdc.GetType();
 int [] array = new int [10];
 Type arrayOfIntsType = array.GetType();
 Type arrayType = typeof(Array);
 
 Console.WriteLine("Array is subclass of int[]? {0}", arrayType.IsSubclassOf(arrayOfIntsType));
 Console.WriteLine("int [] is subclass of Array? {0}", arrayOfIntsType.IsSubclassOf(arrayType));
 Console.WriteLine("IFoo is subclass of IBar? {0}", ifooType.IsSubclassOf(ibarType));
 Console.WriteLine("myclass is subclass of MyClass? {0}", mcType.IsSubclassOf(mcType));
 Console.WriteLine("myderivedclass is subclass of MyClass? {0}", mdcType.IsSubclassOf(mcType));
 Console.WriteLine("IBar is subclass of IFoo? {0}", ibarType.IsSubclassOf(ifooType)); 
  }
}

The output is

Array is subclass of int[]? False
int [] is subclass of Array? True
IFoo is subclass of IBar? False
myclass is subclass of MyClass? False
myderivedclass is subclass of MyClass? True
IBar is subclass of IFoo? False

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0