Recipe13.7.Displaying the Inheritance Hierarchy for a Type


Recipe 13.7. Displaying the Inheritance Hierarchy for a Type

Problem

You need to determine all of the base types that make up a specific type. Essentially, you need to determine the inheritance hierarchy of a type starting with the base (least derived) type and ending with the specified (most derived) type.

Solution

Use the DisplayInheritanceChain method to display the entire inheritance hierarchy for all types existing in an assembly specified by the asmPath parameter. Its source code is:

 public static void DisplayInheritanceChain(string asmPath) {     Assembly asm = Assembly.LoadFrom(asmPath);     foreach(Type type in asm.GetTypes())     {         DisplayInheritanceChain(type);     } } public static void DisplayInheritanceChain(Type type) {     // Recurse over all base types.     Console.WriteLine ("Derived Type: " + type.FullName);     Console.WriteLine ("Base Type List: " + GetBaseTypeList(type));     Console.WriteLine (); } 

DisplayInheritanceChain, in turn, calls GetBaseTypeList, a private method that uses recursion to get all base types. Its source code is:

 private static string GetBaseTypeList(Type type) {     if (type != null)     {         // Recursive method call         string baseTypeName = GetBaseTypeList(type.BaseType);         if (baseTypeName.Length <= 0)         {             return (type.Name);         }         else         {             return (baseTypeName + "<-" + type.Name);         }     }     else     {         return ("");     } } 

If you want to obtain only the inheritance hierarchy of a specific type as a string, use the following DisplayInheritanceChain overload:

 public static void DisplayInheritanceChain(string asmPath, string baseType) {     Assembly asm = Assembly.LoadFrom(asmPath);     DisplayInheritanceChain(asm, baseType); } public static void DisplayInheritanceChain(Assembly asm, string baseType) {     string typeHierarchy = GetBaseTypeList(asm.GetType(baseType));     Console.WriteLine(typeHierarchy); } 

To display the inheritance hierarchy of all types within an assembly, use the first instance of the DisplayInheritanceChain method call. To obtain the inheritance hierarchy of a single type as a string, use the second instance of the DisplayInheritanceChain method call. In this instance, you are looking for the type hierarchy of the CSharpRecipes.ReflectionUtils+DerivedOverrides nested class:

 public static void DisplayInheritanceHierarchyType( ) {     Process current = Process.GetCurrentProcess( );     // Get the path of the current module.     string asmPath = current.MainModule.FileName;     // A specific type     DisplayInheritanceChain(asmPath,         "CSharpRecipes.ReflectionUtils+DerivedOverrides");     // All types in the assembly     DisplayInheritanceChain(asmPath); } 

These methods result in output like the following:

 Derived Type: CSharpRecipes.Reflection Base Type List: Object<-Reflection Derived Type: CSharpRecipes.ReflectionUtils+BaseOverrides Base Type List: Object<-BaseOverrides Derived Type: CSharpRecipes.ReflectionUtils+DerivedOverrides Base Type List: Object<-BaseOverrides <-DerivedOverrides 

This output shows that when looking at the Reflection class in the CSharpRecipes namespace, its base-type list (or inheritance hierarchy) starts with Object (like all types in .NET). The nested class BaseOverrides also shows a base-type list starting with Object. The nested class DerivedOverrides shows a more interesting base-type list, where DerivedOverrides derives from BaseOverrides, which derives from Object.

Discussion

Unfortunately, no property of the Type class exists to obtain the inheritance hierarchy of a type. The DisplayInheritanceChain methods in this recipe allow you to obtain the inheritance hierarchy of a type. All that is required is the path to an assembly and the name of the type with the inheritance hierarchy that is to be obtained. The DisplayInheritanceChain method requires only an assembly path since it displays the inheritance hierarchy for all types within that assembly.

The core code of this recipe exists in the GetBaseTypeList method. This is a recursive method that walks each inherited type until it finds the ultimate base classwhich is always the object class. Once it arrives at this ultimate base class, it returns to its caller. Each time the method returns to its caller, the next base class in the inheritance hierarchy is added to the string until the final GetBaseTypeList method returns the completed string.

See Also

See the "Assembly Class" and "Type.BaseType Method" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net