Export (0) Print
Expand All

Type.GetNestedTypes Method (BindingFlags)

When overridden in a derived class, searches for the types nested in the current Type, using the specified binding constraints.

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

public:
virtual array<Type^>^ GetNestedTypes (
	BindingFlags bindingAttr
) abstract
public abstract Type[] GetNestedTypes (
	BindingFlags bindingAttr
)
public abstract function GetNestedTypes (
	bindingAttr : BindingFlags
) : Type[]

Parameters

bindingAttr

A bitmask comprised of one or more BindingFlags that specify how the search is conducted.

-or-

Zero, to return a null reference (Nothing in Visual Basic).

Return Value

An array of Type objects representing all the types nested in the current Type that match the specified binding constraints (the search is not recursive), or an empty array of type Type, if no nested types are found that match the binding constraints.

The search for nested types is not recursive.

The following BindingFlags filter flags can be used to define which nested types to include in the search:

  • You must specify either BindingFlags.Public or BindingFlags.NonPublic to get a return.

  • Specify BindingFlags.Public to include public nested types in the search.

  • Specify BindingFlags.NonPublic to include non-public nested types (that is, private and protected members) in the search.

This method returns only the nested types of the current type. It does not search the hierarchy of inherited types. To find types that are nested in inherited types, you must walk the inheritance hierarchy.

BindingFlags.Instance and BindingFlags.Static are ignored.

Calling this method with only the BindingFlags.Public flag or only the BindingFlags.NonPublic flag will return the specified nested types and does not require any other flags.

See System.Reflection.BindingFlags for more information.

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 creates two nested public classes and two nested protected classes, and displays information for classes that match the specified binding constraints.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

// Create a class with two nested public classes and two nested protected classes.
public ref class MyTypeClass
{
public:
   ref class Myclass1{};

private:
   ref class Myclass2{};

protected:
   ref class MyClass3{};

private:
   ref class MyClass4{};
};

void DisplayTypeInfo( array<Type^>^myArrayType )
{
   // Display the information for all the nested classes.
   for ( int i = 0; i < myArrayType->Length; i++ )
   {
      Type^ myType = myArrayType[ i ];
      Console::WriteLine( "The name of the nested class is {0}.", myType );

   }
}

int main()
{
   Type^ myType = MyTypeClass::typeid;
   
   // Get the public nested classes.
   array<Type^>^myTypeArray = myType->GetNestedTypes( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
   Console::WriteLine( "The number of nested public classes is {0}.", myTypeArray->Length );
   
   // Display all the public nested classes.
   DisplayTypeInfo( myTypeArray );
   
   // Get the nonpublic nested classes.
   array<Type^>^myTypeArray1 = myType->GetNestedTypes( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) );
   Console::WriteLine( "The number of nested protected classes is {0}.", myTypeArray1->Length );
   
   // Display all the nonpublic nested classes.
   DisplayTypeInfo( myTypeArray1 );
}

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

// Create a class with two nested public classes and 
//two nested protected classes.
public class MyTypeClass
{
    public class Myclass1
    {
    } //Myclass1
   
    public class Myclass2
    {
    } //Myclass2
   
    protected class MyClass3
    {
    } //MyClass3
   
    protected class MyClass4
    {
    } //MyClass4
} //MyTypeClass

public class TypeMain
{
    public static void main(String[] args)
    {
        Type myType = MyTypeClass.class.ToType();
        // Get the public nested classes.
        Type myTypeArray[] = myType.GetNestedTypes(BindingFlags.Public |
            BindingFlags.Instance);
        Console.WriteLine("The number of nested public classes is {0}.",
            (Int32)myTypeArray.get_Length());
        // Display all the public nested classes.
        DisplayTypeInfo(myTypeArray);
        // Get the nonpublic nested classes.
        Type myTypeArray1[] = myType.GetNestedTypes(BindingFlags.NonPublic |
            BindingFlags.Instance);
        Console.WriteLine("The number of nested protected classes is {0}.",
            (Int32)myTypeArray1.get_Length());
        // Display all the nonpublic nested classes.
        DisplayTypeInfo(myTypeArray1);
    } //main

    public static void DisplayTypeInfo(Type myArrayType[])
    {
        // Display the information for all the nested classes.
        for (int i = 0; i < myArrayType.get_Length(); i++) {
            Type myType = (Type)myArrayType.get_Item(i);
            Console.WriteLine("The name of the nested class is {0}.",
                myType.ToString());
        }
    } //DisplayTypeInfo
} //TypeMain

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft