Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Following object inheritance

, 23 Jul 2010
Rate this:
Please Sign up or sign in to vote.
OriginalGriff has made a nice and small class. But I don't think I would use this code in an application. Most times when I want to know about the inheritance chain of an object is while I'm coding. So a quick and nice (commandline?) tool would be a better fit, I think. The problem with...
OriginalGriff has made a nice and small class. But I don't think I would use this code in an application. Most times when I want to know about the inheritance chain of an object is while I'm coding. So a quick and nice (commandline?) tool would be a better fit, I think. The problem with OriginalGriff's solution is that I always have to reference the assembly containing the type. So if I create a tool and then use the "TypeInheritance" class, I will have to add a reference to the assembly containing the type, otherwise it would be impossible to call the constructor.
 
To avoid this problem, I created a (very quick Wink | ;) ) commandline tool based on the parsing capability of the Type.GetType(String) method:
using System;
using System.Collections.Generic;
namespace InheritanceChain
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                Type typeWithUnknownInheritanceChain = Type.GetType(args[0]);
                if (typeWithUnknownInheritanceChain != null)
                {
                    List<Type> listTypes = GetInheritanceChain(typeWithUnknownInheritanceChain);
                    foreach (Type type in listTypes)
                        Console.WriteLine(type.FullName);
                    return;
                }
            }
            ShowHelp();
        }
        static List<Type> GetInheritanceChain(Type type)
        {
            List<Type> listTypes = new List<Type>();
            while (type != null)
            {
                listTypes.Add(type);
                type = type.BaseType;
            }
            listTypes.Reverse();
            return listTypes;
        }
        static void ShowHelp()
        {
            Console.WriteLine("Syntax: InheritanceChain TypeName");
            Console.WriteLine("TypeName\tInheritanceChain see MSDN Help about Type.GetType(String) Method for possible TypeName strings");
            Console.WriteLine("Example: InheritanceChain \"System.Windows.Forms.Form,System.Windows.Forms,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089\"");
        }
    }
}
 
But most times when I work with Visual Studio, I just use the object browser to find out about the inheritance chain.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

johannesnestler

Austria Austria
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 23 Jul 2010
Article Copyright 2010 by johannesnestler
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid