Type.GetNestedTypes 메서드

정의

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

오버로드

Name Description
GetNestedTypes(BindingFlags)

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

GetNestedTypes()

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

GetNestedTypes(BindingFlags)

파생 클래스에서 재정의되는 경우 지정된 바인딩 제약 조건을 사용하여 현재 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

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

-또는-

Default 를 반환 null합니다.

반품

Type[]

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

구현

예제

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

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.Public 사용하여 이 메서드를 BindingFlags.NonPublic 호출하면 지정된 중첩 형식이 반환되며 다른 플래그는 필요하지 않습니다.

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

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

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

메모

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

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

추가 정보

적용 대상

GetNestedTypes()

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

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

반품

Type[]

현재 Type 에 중첩된 public 형식을 나타내는 개체의 Type 배열(검색은 재귀적이지 않음) 또는 현재Type에 중첩된 공용 형식이 없는 경우 형식 Type 의 빈 배열입니다.

구현

예제

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

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 멤버를 보여 줍니다.

멤버 형식 정적 비정적
생성자 No No
Field No Yes. 필드는 항상 이름과 서명을 기준으로 숨겨집니다.
Event 적용할 수 없음 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 이름별 숨기기 및 서명으로 처리합니다. 아래 참고 2를 참조하세요.
Method No Yes. 메서드(가상 및 가상이 아닌 메서드)는 이름으로 숨기기 또는 이름과 서명으로 숨기기가 가능합니다.
중첩 타입 No No
Property 적용할 수 없음 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 이름별 숨기기 및 서명으로 처리합니다. 아래 참고 2를 참조하세요.
  1. 이름별 및 서명에 의한 숨기기는 사용자 지정 한정자, 반환 형식, 매개 변수 형식, 센티넬 및 비관리 호출 규칙을 포함하여 서명의 모든 부분을 고려합니다. 이진 비교입니다.

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

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

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

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

메모

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

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

추가 정보

적용 대상