BlackWaspTM

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

Reflection
.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.

Nested Types

The previous seven articles in this series have concentrated on extracting information about the members of a type. We have looked at the reflection of all types of member, including fields, properties, methods, events, and constructors. In this article we will look at the reflection of nested types, these being classes or structures that are declared within the scope of another type. To do so, we need some example classes that include nested items. We'll use the following code:

namespace NestedTypeReflection
{
    public class OuterClass
    {
        public class NestedClass
        {
            public class NestedNestedClass
            {

            }
        }

        private class PrivateNestedClass
        {

        }
    }
}

The test code above defines four classes. The primary class, "OuterClass" contains two nested classes, one public and one private. The public nested class also contains a further nested class. No members are defined for the classes as these will not be required for the examples and would only complicate the code.

During the examples we'll use types from the System.Reflection and System.Collections namespaces, so add the following using directives to the code:

using System.Reflection;
using System.Collections;

Obtaining a Nested Type Explicitly

In the article, "The System.Type Class" we touched upon the reflection of nested types when we were creating Type instances. We used the typeof command and the Type class' static method, "GetType" to obtain Type objects for various classes, including nested types. The typeof keyword allows us to reflect a class, provided it is in scope, by providing the name of the containing class and the nested type separated by full stops (periods). For example:

Type t1 = typeof(OuterClass.NestedClass);
Type t2 = typeof(OuterClass.NestedClass.NestedNestedClass);

Using typeof has the added advantage of Intellisense support, as Visual Studio is aware of the types defined in your projects. The disadvantage is that is cannot obtain information about types that are out of scope. For example, the following code, added to the Main method, fails to compile because the private nested class is out of scope.

Type t3 = typeof(OuterClass.PrivateNestedClass);

The Type.GetType method uses a string to define the class to be reflected. As such, it does not have the support of Intellisense and the type name cannot be checked for validity at compilation time. However, it does allow private types to be reflected. The following code correctly obtains information for the PrivateNestedClass type. Note that the namespace is required and is separated from the class name by a full stop (period). Types that are nested are separated by plus symbols (+).

Type t3 = Type.GetType("NestedTypeReflection.OuterClass+PrivateNestedClass");

In either case the returned object is an instance of the Type class. The properties of this class have been discussed in an earlier article so I won't repeat them here. They behave identically for nested types as for non-nested types.

Obtaining a Nested Type from a Type

Sometimes you will have a Type instance that represents a class or structure that has nested types that you wish to reflect. You can do this in a similar manner to when you examine a type's members. The basic version GetNestedType method of the Type class allows you to specify the name of the nested type that you wish to obtain. If the nested type exists and is public, a new Type instance that represents it is returned.

As with the methods that obtain members, you can add a BindingFlags parameter to the method in order to retrieve the details of a class' non-public nested types. You can specify that you wish to retrieve the named nested type only if it is public, only if it is non-public or regardless of its scope using combinations of the Public and NonPublic binding flag values. If the nested type does not exist, or if it does not match the binding flags, the method returns null.

The following code obtains a Type instance for OuterClass and then retrieves its two nested types using the GetNestedType method. Note that the name provided to the method does not include the namespace or the name of the outer type.

Type t = typeof(OuterClass);
Type nt = t.GetNestedType("NestedClass");
Type pnt = t.GetNestedType("PrivateNestedClass", BindingFlags.NonPublic);
27 April 2012