This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET 1.1+

Reflecting Nested Type Information

The tenth part of the Reflection tutorial moves away from the reflection of type members and looks are the reflection of nested classes and structures. This article explains how the nested types within a class can be obtained for further interrogation.

Obtaining Multiple Nested Types of a Type

When you want to access the details of all of the nested types within a type you can use the GetNestedTypes method. Again, this method has two overloads. The first accepts no arguments. It returns an array of Type objects, each representing one of the public nested types found. The second overloaded version requires that you provide binding flags, allowing you to obtain all of the nested types or just the public or non-public items. Note that the method only returns nested types that are found directly within the type being examined. It will not find items that are nested more than one level deep.

The following code shows how all of the public and non-public nested types can be found. The final line uses reflection to create a Type object for every top-level nested type in OuterClass.

Type t = typeof(OuterClass);
Type[] allPublicNested = t.GetNestedTypes();
Type[] allNonPublicNested = t.GetNestedTypes(BindingFlags.NonPublic);
Type[] allNested = t.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public);

Recursively Obtaining Nested Types

Although the standard reflection methods do not permit you to list all of the nested classes when there are multiple levels of nesting, it is quite easy to create a new method to get this information. The code below shows a recursive manner for achieving this.

private static Type[] GetAllNestedTypes(Type type)
    ArrayList types = new ArrayList();
    AddNestedTypesRecursively(types, type);
    return (Type[])types.ToArray(typeof(Type));

private static void AddNestedTypesRecursively(ArrayList types, Type type)
    Type[] nestedTypes = type.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public);
    foreach (Type nestedType in nestedTypes)
        AddNestedTypesRecursively(types, nestedType);

The GetAllNestedTypes method is used to initiate the process. It receives an argument containing the type to be reflected. It then creates an empty ArrayList, which will be populated with the nested types, before calling the AddNestedTypesRecursively method. AddNestedTypesRecursively obtains all of the nested types for the provided type and adds them to the ArrayList. For each item added, the method is called again. This recursive calling adds every nested type at every level to the ArrayList. On completion, the ArrayList is converted to an array and returned.

NB: An ArrayList is shown in the example for compatibility with .NET 1.1. For later versions you could use a generic collection type that holds only Type objects.

The final code sample calls GetAllNestedTypes for the OuterClass type and lists all of the nested types that it contains.

Type t = typeof(OuterClass);
Type[] allTypes = GetAllNestedTypes(t);

foreach (Type type in allTypes)



27 April 2012