Type.GetNestedTypes 메서드

정의

현재 Type에 중첩된 형식을 가져옵니다.

오버로드

GetNestedTypes()

현재 Type에 중첩된 public 형식을 반환합니다.

GetNestedTypes(BindingFlags)

파생 클래스에서 재정의되면 지정된 바인딩 제약 조건을 사용하여 현재 Type에 중첩된 형식을 검색합니다.

GetNestedTypes()

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

현재 Type에 중첩된 public 형식을 반환합니다.

public:
 cli::array <Type ^> ^ GetNestedTypes();
public:
 virtual cli::array <Type ^> ^ GetNestedTypes();
public Type[] GetNestedTypes ();
member this.GetNestedTypes : unit -> Type[]
abstract member GetNestedTypes : unit -> Type[]
override this.GetNestedTypes : unit -> Type[]
Public Function GetNestedTypes () As Type()

반환

Type[]

현재 Type에 중첩된 public 형식을 나타내는 Type 개체의 배열이거나(검색은 재귀적이 아님), 현재 Type에 중첩된 public 형식이 없으면 Type 형식의 빈 배열입니다.

구현

예제

다음 예제에서는 의 중첩 클래스와 를 structMyClass정의한 다음 형식을 사용하여 중첩된 형식의 개체를 MyClass가져옵니다.

using namespace System;
using namespace System::Reflection;
public ref class MyClass
{
public:
   ref class NestClass
   {
      public:
         static int myPublicInt = 0;
   };

   ref struct NestStruct
   {
      public:
         static int myPublicInt = 0;
   };
};

int main()
{
   try
   {
      // Get the Type object corresponding to MyClass.
      Type^ myType = MyClass::typeid;
      
      // Get an array of nested type objects in MyClass.
      array<Type^>^nestType = myType->GetNestedTypes();
      Console::WriteLine( "The number of nested types is {0}.", nestType->Length );
      System::Collections::IEnumerator^ myEnum = nestType->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Type^ t = safe_cast<Type^>(myEnum->Current);
         Console::WriteLine( "Nested type is {0}.", t );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Error {0}", e->Message );
   }
}
using System;
using System.Reflection;
public class MyClass
{
    public class NestClass
    {
        public static int myPublicInt=0;
    }
    public struct NestStruct
    {
        public static int myPublicInt=0;
    }
}

public class MyMainClass
{
    public static void Main()
    {
        try
        {
            // Get the Type object corresponding to MyClass.
            Type myType=typeof(MyClass);
            // Get an array of nested type objects in MyClass.
            Type[] nestType=myType.GetNestedTypes();
            Console.WriteLine("The number of nested types is {0}.", nestType.Length);
            foreach(Type t in nestType)
                Console.WriteLine("Nested type is {0}.", t.ToString());
        }
        catch(Exception e)
        {
            Console.WriteLine("Error"+e.Message);
        }
    }
}
Imports System.Reflection

Public Class MyClass1
    Public Class NestClass
        Public Shared myPublicInt As Integer = 0
    End Class

    Public Structure NestStruct
        Public myPublicInt As Integer
    End Structure 'NestStruct
End Class

Public Class MyMainClass
    Public Shared Sub Main()
        Try
            ' Get the Type object corresponding to MyClass.
            Dim myType As Type = GetType(MyClass1)
            ' Get an array of nested type objects in MyClass.                 
            Dim nestType As Type() = myType.GetNestedTypes()
            Console.WriteLine("The number of nested types is {0}.", nestType.Length)
            Dim t As Type
            For Each t In nestType
                Console.WriteLine("Nested type is {0}.", t.ToString())
            Next t
        Catch e As Exception
            Console.WriteLine("Error", e.Message.ToString())
        End Try
    End Sub
End Class

설명

.NET 6 및 이전 버전 GetNestedTypes 에서 메서드는 사전순 또는 선언 순서와 같은 특정 순서로 형식을 반환하지 않습니다. 코드는 해당 순서가 다르기 때문에 형식이 반환되는 순서에 의존해서는 안 됩니다. 그러나 .NET 7부터는 어셈블리의 메타데이터 순서에 따라 순서가 결정적입니다.

현재 형식에 즉시 중첩된 public 형식만 반환됩니다. 검색이 재귀적이지 않습니다.

다음 표에서는 형식을 반영할 때 메서드에서 반환되는 기본 클래스의 Get 멤버를 보여 줍니다.

멤버 형식 정적 비정적
생성자 아니요 아니요
필드 아니요 예. 필드는 항상 이름별 및 서명으로 숨깁니다.
이벤트 해당 없음 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 hide-by-name-and-signature으로 처리합니다. 아래의 참고 2를 참조하세요.
메서드 아니요 예. 메서드(가상 및 가상이 아닌 메서드)는 이름별 숨기기 또는 이름별 숨기기 및 서명일 수 있습니다.
중첩 형식 아니요 아니요
속성 해당 없음 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 hide-by-name-and-signature으로 처리합니다. 아래의 참고 2를 참조하세요.
  1. 이름별 숨기기 및 서명은 사용자 지정 한정자, 반환 형식, 매개 변수 형식, sentinels 및 관리되지 않는 호출 규칙을 포함하여 서명의 모든 부분을 고려합니다. 이진 비교입니다.

  2. 리플렉션의 경우 속성 및 이벤트는 이름별 숨기기 및 서명입니다. 기본 클래스에 get 및 set 접근자가 둘 다 있는 속성이 있지만 파생 클래스에 get 접근자만 있는 경우 파생 클래스 속성은 기본 클래스 속성을 숨기며 기본 클래스의 setter에 액세스할 수 없습니다.

  3. 사용자 지정 특성은 공통 형식 시스템의 일부가 아닙니다.

현재 Type 가 제네릭 형식 또는 제네릭 메서드의 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 중첩된 형식을 검색합니다.

중첩된 형식이 제네릭인 경우 이 메서드는 제네릭 형식 정의를 반환합니다. 바깥쪽 제네릭 형식이 닫힌 생성 형식인 경우에도 마찬가지입니다.

참고

현재 Type 가 C#, Visual Basic 또는 C++에 정의된 제네릭 형식을 나타내는 경우 중첩된 형식은 고유한 제네릭 매개 변수가 없더라도 모두 제네릭 형식입니다. 동적 어셈블리에 정의되거나 Ilasm.exe(IL 어셈블러)로 컴파일된 중첩 형식의 경우 반드시 해당되는 것은 아닙니다.

중첩된 제네릭 형식 및 제네릭 형식 정의에서 중첩된 제네릭 형식을 생성하는 방법에 대한 자세한 내용은 를 참조하세요 MakeGenericType.

추가 정보

적용 대상

GetNestedTypes(BindingFlags)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

파생 클래스에서 재정의되면 지정된 바인딩 제약 조건을 사용하여 현재 Type에 중첩된 형식을 검색합니다.

public:
 abstract cli::array <Type ^> ^ GetNestedTypes(System::Reflection::BindingFlags bindingAttr);
public abstract Type[] GetNestedTypes (System.Reflection.BindingFlags bindingAttr);
abstract member GetNestedTypes : System.Reflection.BindingFlags -> Type[]
Public MustOverride Function GetNestedTypes (bindingAttr As BindingFlags) As Type()

매개 변수

bindingAttr
BindingFlags

검색 방법을 지정하는 열거형 값의 비트 조합입니다.

또는

null을 반환하는 Default입니다.

반환

Type[]

지정된 바인딩 제약 조건과 일치하는 현재 Type에 중첩된 모든 형식을 나타내는 Type 개체의 배열이거나(검색은 재귀적이 아님), 바인딩 제약 조건과 일치하는 중첩 형식이 없으면 Type 형식의 빈 배열입니다.

구현

예제

다음 예제에서는 두 개의 중첩된 공용 클래스와 두 개의 중첩된 보호된 클래스를 만들고 지정된 바인딩 제약 조건과 일치하는 클래스에 대한 정보를 표시합니다.

using namespace System;
using namespace System::Reflection;

// Create a class with two nested public classes and two nested protected classes.
public ref class MyTypeClass
{
   public:
      ref class Myclass1{};
   
   public:
      ref class Myclass2{};
   
   protected:
      ref class MyClass3{};
   
   protected:
      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 );
   Console::WriteLine();
   
   // 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.
using System;
using System.Reflection;

// Create a class with 2 nested public and 2 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);
        Console.WriteLine("The number of nested public classes is {0}.", myTypeArray.Length);
        // Display all the public nested classes.
        DisplayTypeInfo(myTypeArray);
        Console.WriteLine();

        // 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.
        foreach (var t in myArrayType)
            Console.WriteLine("The name of the nested class is {0}.", t.FullName);
    }
}
// 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.
Imports System.Reflection

' Create a class with three properties.
Public Class MyTypeClass
    Public Class Myclass1
    End Class 

    Public Class Myclass2
    End Class 

    Protected Class MyClass3
    End Class 

    Protected Class MyClass4
    End Class 
End Class 

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))
        Console.WriteLine("The number of public nested classes is {0}.", myTypeArray.Length.ToString())
        ' Display all the public nested classes.
        DisplayTypeInfo(myTypeArray)
        Console.WriteLine()
        
        ' Get the nonpublic nested classes.
        Dim myTypeArray1 As Type() = myType.GetNestedTypes((BindingFlags.NonPublic))
        Console.WriteLine("The number of protected nested classes is {0}.", myTypeArray1.Length.ToString())
        ' Display  the information for all nested classes.
        DisplayTypeInfo(myTypeArray1)
    End Sub 

    Public Shared Sub DisplayTypeInfo(ByVal myArrayType() As Type)
        ' Display the information for all nested classes.
        For Each t In myArrayType
            Console.WriteLine("The name of the nested class is {0}.", t.FullName)
        Next 
    End Sub 
End Class  
' 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 6 및 이전 버전 GetNestedTypes 에서 메서드는 사전순 또는 선언 순서와 같은 특정 순서로 형식을 반환하지 않습니다. 코드는 해당 순서가 다르기 때문에 형식이 반환되는 순서에 의존해서는 안 됩니다. 그러나 .NET 7부터는 어셈블리의 메타데이터 순서에 따라 순서가 결정적입니다.

다음 BindingFlags 필터 플래그를 사용하여 검색에 포함할 중첩 형식을 정의할 수 있습니다.

이 메서드는 현재 형식의 중첩된 형식만 반환합니다. 현재 형식의 기본 클래스를 검색하지 않습니다. 기본 클래스에 중첩된 형식을 찾으려면 상속 계층 구조를 각 수준에서 호출 GetNestedTypes 하여 걸어야 합니다.

BindingFlags.InstanceBindingFlags.Static은 무시됩니다.

플래그 또는 플래그만 사용하여 이 메서드를 BindingFlags.PublicBindingFlags.NonPublic 호출하면 지정된 중첩 형식이 반환되며 다른 플래그가 필요하지 않습니다.

자세한 내용은 System.Reflection.BindingFlags를 참조하세요.

현재 Type 가 제네릭 형식 또는 제네릭 메서드의 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 중첩된 형식을 검색합니다.

중첩된 형식이 제네릭인 경우 이 메서드는 제네릭 형식 정의를 반환합니다. 바깥쪽 제네릭 형식이 닫힌 생성 형식인 경우에도 마찬가지입니다.

참고

현재 Type 가 C#, Visual Basic 또는 C++에 정의된 제네릭 형식을 나타내는 경우 중첩된 형식은 고유한 제네릭 매개 변수가 없더라도 모두 제네릭 형식입니다. 동적 어셈블리에 정의되거나 Ilasm.exe(IL 어셈블러)로 컴파일된 중첩 형식의 경우 반드시 해당되는 것은 아닙니다.

중첩된 제네릭 형식 및 제네릭 형식 정의에서 중첩된 제네릭 형식을 생성하는 방법에 대한 자세한 내용은 를 참조하세요 MakeGenericType.

추가 정보

적용 대상