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)

virtual array<Type^>^ GetNestedTypes(
	BindingFlags bindingAttr
) abstract


Type: System.Reflection::BindingFlags

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


Zero, to return null.

Return Value

Type: array<System::Type^>^

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

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

This method returns only the nested types of the current type. It does not search the base classes of the current type. To find types that are nested in base classes, you must walk the inheritance hierarchy, calling GetNestedTypes at each level.

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.


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 Ilasm.exe (IL Assembler).

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;

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

      ref class Myclass2{};

      ref class MyClass3{};

      ref class MyClass4{};

void DisplayTypeInfo(array<Type^>^ myArrayType)
   // Display the information for all the nested classes.
   for each (Type^ t in myArrayType)
      Console::WriteLine( "The name of the nested class is {0}.", t->FullName);

int main()
   Type^ myType = MyTypeClass::typeid;

   // Get the public nested classes.
   array<Type^>^myTypeArray = myType->GetNestedTypes( static_cast<BindingFlags>(BindingFlags::Public));
   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));
   Console::WriteLine( "The number of nested protected classes is {0}.", myTypeArray1->Length );

   // Display all the nonpublic nested classes.
   DisplayTypeInfo( myTypeArray1 );
// The example displays the following output:
//       The number of public nested classes is 2.
//       The name of the nested class is MyTypeClass+Myclass1.
//       The name of the nested class is MyTypeClass+Myclass2.
//       The number of protected nested classes is 2.
//       The name of the nested class is MyTypeClass+MyClass3.
//       The name of the nested class is MyTypeClass+MyClass4.

.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top