How to Display Inheritance Tree of Any Type in C#

In the complex application, where multiple classes are involved, sometimes you need to check inheritance tree structure of types. It might help you to fix your bug or restructure your application classes architecture.

Let’s see how you can display inheritance structure of any type using C# code.

Display Inheritance Tree in C#

Create a Console application in Visual Studio (C#).

Write below code into your main function. Build application and run it. You will get inheritance tree structure.

This code is all about to get all parent classes of type int[] (integer array).

      static void Main(string[] args)
            var numbers = new int[] {10, 20, 30, 40 , 50};
            var type = numbers.GetType();

            Console.WriteLine("Following is the inheritance Tree of Type "+ type.FullName + " from Child to Parent:\n");

               type = type.BaseType;
            while (type != null);

In the above code, we have declared integer array. In the next lines of code we are displaying Full Name of original type and looping through it’s Base Types until end of its inheritance tree.

Hope this is helpful.

Subscribe Now!

Subscribe Us For Latest Articles