Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Type.GetNestedTypes Method ()

Returns the public types nested in the current Type.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public Type[] GetNestedTypes ()
public final Type[] GetNestedTypes ()
public final function GetNestedTypes () : Type[]
Not applicable.

Return Value

An array of Type objects representing the public types nested in the current Type (the search is not recursive), or an empty array of type Type if no public types are nested in the current Type.

The GetNestedTypes method does not return types in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which types are returned, because that order varies.

Only the public types immediately nested in the current type are returned; the search is not recursive.

The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

Member Type

Static

Non-Static

Constructor

No

No

Field

No

Yes. A field is always hide-by-name-and-signature.

Event

Not applicable

The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.

Method

No

Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.

Nested Type

No

No

Property

Not applicable

The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.

  1. Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. This is a binary comparison.

  2. For reflection, properties and events are hide-by-name-and-signature. If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. Custom attributes are not part of the common type system.

If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the nested types of the class constraint.

If a nested type is generic, this method returns its generic type definition. This is true even if the enclosing generic type is a closed constructed type.

NoteNote:

If the current Type represents a generic type defined in C#, Visual Basic, or C++, its nested types are all generic even if they have no generic parameters of their own. This is not necessarily true of nested types defined in dynamic assemblies or compiled with the MSIL Assembler (Ilasm.exe).

For information on nested generic types, and on constructing nested generic types from their generic type definitions, see MakeGenericType.

The following example defines a nested class and a struct in MyClass, and then obtains objects of the nested types using the type of MyClass.


using System;
using System.Reflection;
public class MyClass 
{
    public class NestClass 
    {
        public static int myPublicInt=0;
    }
    public struct NestStruct
    {
        public static int myPublicInt=0;
    }
}

public class MyMainClass 
{
    public static void Main() 
    {
        try
        {
            // Get the Type object corresponding to MyClass.
            Type myType=typeof(MyClass);
            // Get an array of nested type objects in MyClass. 
            Type[] nestType=myType.GetNestedTypes();
            Console.WriteLine("The number of nested types is {0}.", nestType.Length);
            foreach(Type t in nestType)
                Console.WriteLine("Nested type is {0}.", t.ToString());
        }
        catch(Exception e)
        {
            Console.WriteLine("Error"+e.Message);  
        }         
    }
}

import System.*;
import System.Reflection.*;

public class MyClass
{
    public static class NestClass
    {
        public static int myPublicInt = 0;
    } //NestClass

    public static class NestStruct
    {
        public static int myPublicInt = 0;
    } //NestStruct
} //MyClass

public class MyMainClass
{
    public static void main(String[] args)
    {
        try {
            // Get the Type object corresponding to MyClass.
            Type myType = MyClass.class.ToType();
            // Get an array of nested type objects in MyClass. 
            Type nestType[] = myType.GetNestedTypes();
            Console.WriteLine("The number of nested types is {0}.",
                System.Convert.ToString(nestType.get_Length()));
            for (int iCtr = 0; iCtr < nestType.get_Length(); iCtr++) {
                Type t = nestType[iCtr];
                Console.WriteLine("Nested type is {0}.", t.ToString());
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("Error" + e.get_Message());
        }
    } //main
} //MyMainClass

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft