Export (0) Print
Expand All

Type.GetNestedTypes Method (BindingFlags)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public MustOverride Function GetNestedTypes( _
   ByVal bindingAttr As BindingFlags _
) As Type()
[C#]
public abstract Type[] GetNestedTypes(
 BindingFlags bindingAttr
);
[C++]
public: virtual Type* GetNestedTypes(
 BindingFlags bindingAttr
) [] = 0;
[JScript]
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 within the current Type that match the specified binding constraints.

-or-

An empty array of type Type, if no types are nested within the current Type, or if none of the nested types match the binding constraints.

Remarks

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

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order 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.
  • Specify BindingFlags.FlattenHierarchy to include static nested types up the hierarchy.

The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly to search only the nested types declared on the Type, not nested types that were simply inherited.

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

See System.Reflection.BindingFlags for more information.

Example

[Visual Basic, C#, C++] The following example creates two nested public classes and two nested protected classes and displays information for classes that match the specified binding constraints.

[Visual Basic] 

Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports Microsoft.VisualBasic

' Create a class with name 'MyTypeClass' with three properties.
Public Class MyTypeClass

    Public Class Myclass1
    End Class 'Myclass1


    Public Class Myclass2
    End Class 'Myclass2


    Protected Class MyClass3
    End Class 'MyClass3

    Protected Class MyClass4
    End Class 'MyClass4
End Class 'MyTypeClass


Public Class TypeMain

    Public Shared Sub Main()

        Dim myType As Type = GetType(MyTypeClass)
        ' Get the public nested classes.
        Dim myTypeArray As Type() = myType.GetNestedTypes((BindingFlags.Public Or BindingFlags.Instance))
        Console.WriteLine("The number of public nested classes is {0}.", myTypeArray.Length.ToString())
        ' Display all the public nested classes.
        DisplayTypeInfo(myTypeArray)
        ' Get the nonpublic nested classes.
        Dim myTypeArray1 As Type() = myType.GetNestedTypes((BindingFlags.NonPublic Or BindingFlags.Instance))
        Console.WriteLine("The number of protected nested classes is {0}.", myTypeArray1.Length.ToString())
        ' Display  the information for all nested classes.
        DisplayTypeInfo(myTypeArray1)
    End Sub 'Main

    Public Shared Sub DisplayTypeInfo(ByVal myArrayType() As Type)
        ' Display the information for all nested classes.
        Dim i As Integer
        For i = 0 To myArrayType.Length - 1
            Dim myType As Type = CType(myArrayType(i), Type)
            Console.WriteLine("The name of the nested class is {0}.", myType.ToString())
        Next i
    End Sub 'DisplayTypeInfo
End Class 'TypeMain 

[C#] 

using System;
using System.Reflection;
using System.Reflection.Emit;

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

public class TypeMain
{
    public static void Main() 
    {
        Type myType =(typeof(MyTypeClass));
        // Get the public nested classes.
        Type[] myTypeArray = myType.GetNestedTypes(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.
        Type[] myTypeArray1 = myType.GetNestedTypes(BindingFlags.NonPublic|BindingFlags.Instance);
        Console.WriteLine("The number of nested protected classes is {0}.", myTypeArray1.Length);
        // Display all the nonpublic nested classes.
        DisplayTypeInfo(myTypeArray1);        
    }
    public static void DisplayTypeInfo(Type[] myArrayType)
    {
        // Display the information for all the nested classes.
        for(int i=0;i<myArrayType.Length;i++)
        {
            Type myType = (Type)myArrayType[i];
            Console.WriteLine("The name of the nested class is {0}.", myType.ToString());
        }
    }

[C++] 

#using <mscorlib.dll>

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 __gc class MyTypeClass {
public:
   __gc class Myclass1 {
   };
   __gc class Myclass2 {
   };
protected:
   __gc class MyClass3 {
   };
   __gc class MyClass4 {
   };
};

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

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Type Class | Type Members | System Namespace | Type.GetNestedTypes Overload List | BindingFlags | DefaultBinder | GetNestedType

Show:
© 2014 Microsoft