共用方式為


Type.GetNestedTypes Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public MustOverride Function GetNestedTypes ( _
    bindingAttr As BindingFlags _
) As Type()
public abstract Type[] GetNestedTypes(
    BindingFlags bindingAttr
)

Parameters

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.

Remarks

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:

  • 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 nested types) 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.

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 Microsoft intermediate language (MSIL) assembler.

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

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 GetNestedTypes returns all nested types regardless of the BindingFlags value.

Examples

The following example creates two nested public classes and two nested protected classes, and displays information for classes that match the specified binding constraints.


Imports System.Reflection
Imports System.Reflection.Emit

' 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 Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

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

   Public Shared Sub DisplayTypeInfo(ByVal outputBlock As System.Windows.Controls.TextBlock, 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)
         outputBlock.Text += String.Format("The name of the nested class is {0}.", myType.ToString()) & vbCrLf
      Next i
   End Sub 'DisplayTypeInfo
End Class 'TypeMain 

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 Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Type myType = (typeof(MyTypeClass));
      // Get the public nested classes.
      Type[] myTypeArray = myType.GetNestedTypes(BindingFlags.Public | BindingFlags.Instance);
      outputBlock.Text += String.Format("The number of nested public classes is {0}.", myTypeArray.Length) + "\n";
      // Display all the public nested classes.
      DisplayTypeInfo(outputBlock, myTypeArray);
      // Get the nonpublic nested classes.
      Type[] myTypeArray1 = myType.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Instance);
      outputBlock.Text += String.Format("The number of nested protected classes is {0}.", myTypeArray1.Length) + "\n";
      // Display all the nonpublic nested classes.
      DisplayTypeInfo(outputBlock, myTypeArray1);
   }
   public static void DisplayTypeInfo(System.Windows.Controls.TextBlock outputBlock, Type[] myArrayType)
   {
      // Display the information for all the nested classes.
      for (int i = 0; i < myArrayType.Length; i++)
      {
         Type myType = (Type)myArrayType[i];
         outputBlock.Text += String.Format("The name of the nested class is {0}.", myType.ToString()) + "\n";
      }
   }

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.