Click here to Skip to main content
15,890,897 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Following object inheritance

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
23 Jul 2010CPOL 4.1K   1  
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 ;) ) 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)


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --