Get the type from which the current Type directly inherits in CSharp

Description

The following code shows how to get the type from which the current Type directly inherits.

Example


using System;/*from  w w  w .  jav a2s  . co  m*/
class TestType 
{
    public static void Main() 
    {
        Type tt = typeof(int);
        Console.WriteLine("{0} inherits from {1}.", tt,tt.BaseType);

        foreach (var t in typeof(TestType).Assembly.GetTypes())
        {
         Console.WriteLine("{0} derived from: ", t.FullName);
         var derived = t;
         do { 
            derived = derived.BaseType;
            if (derived != null) 
               Console.WriteLine("   {0}", derived.FullName);

         } while (derived != null);
         Console.WriteLine(); 
      } 
   }
}

public class A {} 

public class B : A
{}

public class C : B   
{}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type