Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Type.GetNestedTypes Method

Gets the types nested within the current Type.

Overload List

Returns all the types nested within the current Type.

[Visual Basic] Overloads Public Function GetNestedTypes() As Type()
[C#] public Type[] GetNestedTypes();
[C++] public: Type* GetNestedTypes() [];
[JScript] public function GetNestedTypes() : Type[];

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public MustOverride Function GetNestedTypes(BindingFlags) As Type()
[C#] public abstract Type[] GetNestedTypes(BindingFlags);
[C++] public: virtual Type* GetNestedTypes(BindingFlags) [] = 0;
[JScript] public abstract function GetNestedTypes(BindingFlags) : Type[];

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, C#, C++] Note   This example shows how to use one of the overloaded versions of GetNestedTypes. For other examples that might be available, see the individual overload topics.
[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.

See Also

Type Class | Type Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.