Following object inheritance





5.00/5 (1 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 ;) ) 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.