DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 12.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 DisplayTypeHierarchy 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 DisplayTypeHierarchy (string asmPath)
{
    Assembly asm = Assembly.LoadFrom(asmPath);
    foreach(Type asmType in asm.GetTypes( ))
    {
        // Recurse over all base types
        Console.WriteLine ("Derived Type: " + asmType.FullName);
        Console.WriteLine ("Base Type List: " + GetBaseTypeList(asmType));
        Console.WriteLine ( );
    }
}

DisplayTypeHierarchy 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)
    {
        string baseTypeName = GetBaseType(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 DisplayTypeHierarchy overload:

public static void DisplayTypeHierarchy(string asmPath,string baseType)
{
    Assembly asm = Assembly.LoadFrom(asmPath);
    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 DisplayTypeHierarchy method call. To obtain the inheritance hierarchy of a single type as a string, use the second instance of the DisplayTypeHierarchy method call. In this instance, we are looking for the type hierarchy of the CSharpRecipes.Reflection+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
    TypeHierarchy.DisplayTypeHierarchy(asmPath,
        "CSharpRecipes.Reflection+DerivedOverrides");
    // all types in the assembly
    TypeHierarchy.DisplayTypeHierarchy(asmPath);     
}

These methods result in output like the following:

Derived Type: CSharpRecipes.Reflection
Base Type List: Object::Reflection    

Derived Type: CSharpRecipes.Reflection+BaseOverrides
Base Type List: Object::BaseOverrides

Derived Type: CSharpRecipes.Reflection+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 DisplayTypeHierarchy 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 whose inheritance hierarchy is to be obtained. The DisplayTypeHierarchy method requires only an assembly path since it displays only 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 class—which 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 "Process Class," "Assembly Class," and "Type.BaseType Method" topics in the MSDN documentation.

    [ Team LiB ] Previous Section Next Section