Type.GetMembers 메서드

정의

현재 Type의 멤버(속성, 메서드, 필드, 이벤트 등)를 가져옵니다.

오버로드

GetMembers(BindingFlags)

파생 클래스에서 재정의되면, 현재 Type에 대해 정의된 멤버를 지정된 바인딩 제약 조건으로 검색합니다.

GetMembers()

현재 Type의 모든 public 멤버를 반환합니다.

GetMembers(BindingFlags)

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

파생 클래스에서 재정의되면, 현재 Type에 대해 정의된 멤버를 지정된 바인딩 제약 조건으로 검색합니다.

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

매개 변수

bindingAttr
BindingFlags

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

또는

빈 배열을 반환하는 Default입니다.

반환

현재 MemberInfo에 대해 정의된 필드 중 지정된 바인딩 제약 조건과 일치하는 모든 멤버를 나타내는 Type 개체의 배열입니다.

또는

현재 Type에 대해 정의된 멤버가 없거나 정의된 멤버 중 바인딩 제약 조건과 일치하는 멤버가 없을 경우 빈 배열입니다.

구현

예제

다음 코드 예제에서는 메서드 오버로드를 사용 하 여 GetMembers(BindingFlags) 지정 된 클래스의 모든 공용 instance 멤버에 대 한 정보를 수집 하는 방법을 보여 줍니다.

ref class MyClass
{
public:
   int * myInt;
   String^ myString;
   MyClass(){}

   void Myfunction(){}

};

int main()
{
   try
   {
      MyClass^ MyObject = gcnew MyClass;
      array<MemberInfo^>^myMemberInfo;
      
      // Get the type of the class 'MyClass'.
      Type^ myType = MyObject->GetType();
      
      // Get the public instance members of the class 'MyClass'.
      myMemberInfo = myType->GetMembers( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
      Console::WriteLine( "\nThe public instance members of class '{0}' are : \n", myType );
      for ( int i = 0; i < myMemberInfo->Length; i++ )
      {
         
         // Display name and type of the member of 'MyClass'.
         Console::WriteLine( "'{0}' is a {1}", myMemberInfo[ i ]->Name, myMemberInfo[ i ]->MemberType );

      }
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException : {0}", e->Message );
   }


      //Output:
      //The public instance members of class 'MyClass' are :

      //'Myfunction' is a Method
      //'ToString' is a Method
      //'Equals' is a Method
      //'GetHashCode' is a Method
      //'GetType' is a Method
      //'.ctor' is a Constructor
      //'myInt' is a Field
      //'myString' is a Field

}

class MyClass
{
   public int myInt = 0;
   public string myString = null;

   public MyClass()
   {
   }
   public void Myfunction()
   {
   }
}

class Type_GetMembers_BindingFlags
{
   public static void Main()
   {
      try
      {
         MyClass MyObject = new MyClass();
         MemberInfo [] myMemberInfo;

         // Get the type of the class 'MyClass'.
         Type myType = MyObject.GetType();

         // Get the public instance members of the class 'MyClass'.
         myMemberInfo = myType.GetMembers(BindingFlags.Public|BindingFlags.Instance);

         Console.WriteLine( "\nThe public instance members of class '{0}' are : \n", myType);
         for (int i =0 ; i < myMemberInfo.Length ; i++)
         {
            // Display name and type of the member of 'MyClass'.
            Console.WriteLine( "'{0}' is a {1}", myMemberInfo[i].Name, myMemberInfo[i].MemberType);
         }
      }
      catch (SecurityException e)
      {
         Console.WriteLine("SecurityException : " + e.Message );
      }

      //Output:
      //The public instance members of class 'MyClass' are :

      //'Myfunction' is a Method
      //'ToString' is a Method
      //'Equals' is a Method
      //'GetHashCode' is a Method
      //'GetType' is a Method
      //'.ctor' is a Constructor
      //'myInt' is a Field
      //'myString' is a Field
   }
}
open System.Reflection
open System.Security

type MyClass =
    val public myInt: int
    val public myString: string

    new () = { myInt = 0; myString = null}

    member _.MyMethod() = ()

try
    let MyObject = MyClass()

    // Get the type of the class 'MyClass'.
    let myType = MyObject.GetType()

    // Get the public instance members of the class 'MyClass'.
    let myMemberInfo = myType.GetMembers(BindingFlags.Public ||| BindingFlags.Instance)

    printfn $"\nThe public instance members of class '{myType}' are : \n"
    for i = 0 to myMemberInfo.Length - 1 do
        // Display name and type of the member of 'MyClass'.
        printfn $"'{myMemberInfo[i].Name}' is a {myMemberInfo[i].MemberType}"
with :? SecurityException as e ->
    printfn $"SecurityException : {e.Message}"

//Output:
//The public instance members of class 'MyClass' are :

//'Myfunction' is a Method
//'ToString' is a Method
//'Equals' is a Method
//'GetHashCode' is a Method
//'GetType' is a Method
//'.ctor' is a Constructor
//'myInt' is a Field
//'myString' is a Field
Class [MyClass]
   Public myInt As Integer = 0
   Public myString As String = Nothing
   
   
   Public Sub New()
   End Sub
   
   Public Sub Myfunction()
   End Sub
End Class

Class Type_GetMembers_BindingFlags
   
   Public Shared Sub Main()
      Try
         Dim MyObject As New [MyClass]()
         Dim myMemberInfo() As MemberInfo
         
         ' Get the type of the class 'MyClass'.
         Dim myType As Type = MyObject.GetType()
         
         ' Get the public instance members of the class 'MyClass'. 
         myMemberInfo = myType.GetMembers((BindingFlags.Public Or BindingFlags.Instance))
         
         Console.WriteLine(ControlChars.Cr + "The public instance members of class '{0}' are : " + ControlChars.Cr, myType)
         Dim i As Integer
         For i = 0 To myMemberInfo.Length - 1
            ' Display name and type of the member of 'MyClass'.
            Console.WriteLine("'{0}' is a {1}", myMemberInfo(i).Name, myMemberInfo(i).MemberType)
         Next i
      
      Catch e As SecurityException
         Console.WriteLine(("SecurityException : " + e.Message.ToString()))
      End Try


      'Output:
      'The public instance members of class 'MyClass' are :

      ''Myfunction' is a Method
      ''ToString' is a Method
      ''Equals' is a Method
      ''GetHashCode' is a Method
      ''GetType' is a Method
      ''.ctor' is a Constructor
      ''myInt' is a Field
      ''myString' is a Field


   End Sub
End Class

설명

멤버에는 속성, 메서드, 생성자, 필드, 이벤트 및 중첩 형식이 포함됩니다.

오버로드가 GetMethods(BindingFlags) 메서드 정보를 bindingAttr 성공적으로 검색하려면 인수에 및 중 하나 BindingFlags.Instance 이상과 BindingFlags.StaticBindingFlags.PublicBindingFlags.NonPublic 하나 이상이 포함되어야 합니다. 유일한 예외는 중첩된 형식에 대한 멤버 정보를 반환하는 를 사용하는 메서드 호출 BindingFlags.NonPublic입니다.

다음 BindingFlags 필터 플래그를 사용하여 검색에 포함할 멤버를 정의할 수 있습니다.

  • instance 메서드를 포함하도록 지정 BindingFlags.Instance 합니다.

  • 정적 메서드를 포함하도록 지정 BindingFlags.Static 합니다.

  • 검색에 공용 메서드를 포함하도록 지정 BindingFlags.Public 합니다.

  • 비공용 메서드(즉, 프라이빗, 내부 및 보호된 메서드)를 검색에 포함하도록 지정 BindingFlags.NonPublic 합니다. 기본 클래스의 보호된 메서드와 내부 메서드만 반환됩니다. 기본 클래스의 private 메서드는 반환되지 않습니다.

  • 계층 구조에 멤버를 포함 public 하도록 지정하고 protected 정적 멤버를 지정 BindingFlags.FlattenHierarchy 합니다private. 상속된 클래스의 정적 멤버는 포함되지 않습니다.

  • MethodInfo 배열을 반환하려면 단독으로 지정 BindingFlags.Default 합니다.

다음 BindingFlags 한정자 플래그를 사용하여 검색 작동 방식을 변경할 수 있습니다.

  • BindingFlags.DeclaredOnly 에 선언된 Type멤버만 검색하려면 단순히 상속된 멤버가 아닙니다.

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

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

이 메서드 오버로드를 사용하여 클래스 이니셜라이저(정적 생성자)를 얻으려면 ( Visual Basic에서)를BindingFlags.NonPublicBindingFlags.StaticOr 지정 BindingFlags.Static | BindingFlags.NonPublic 해야 합니다. 속성을 사용하여 클래스 이니셜라이저를 TypeInitializer 가져올 수도 있습니다.

현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 형식 매개 변수가 적절한 형식 인수로 대체된 개체를 반환 MemberInfo 합니다.

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

추가 정보

적용 대상

GetMembers()

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

현재 Type의 모든 public 멤버를 반환합니다.

public:
 cli::array <System::Reflection::MemberInfo ^> ^ GetMembers();
public:
 virtual cli::array <System::Reflection::MemberInfo ^> ^ GetMembers();
public System.Reflection.MemberInfo[] GetMembers ();
member this.GetMembers : unit -> System.Reflection.MemberInfo[]
abstract member GetMembers : unit -> System.Reflection.MemberInfo[]
override this.GetMembers : unit -> System.Reflection.MemberInfo[]
Public Function GetMembers () As MemberInfo()

반환

현재 MemberInfo의 모든 public 멤버를 나타내는 Type 개체의 배열입니다.

또는

현재 MemberInfo에 public 멤버가 없을 경우 Type 형식의 빈 배열입니다.

구현

예제

다음 코드 예제에서는 메서드 오버로드를 사용하여 GetMembers() 지정된 클래스의 모든 공용 멤버에 대한 정보를 수집하는 방법을 보여 줍니다.

ref class MyClass
{
public:
   int myInt;
   String^ myString;
   MyClass(){}

   void Myfunction(){}

};

int main()
{
   try
   {
      MyClass^ myObject = gcnew MyClass;
      array<MemberInfo^>^myMemberInfo;
      
      // Get the type of 'MyClass'.
      Type^ myType = myObject->GetType();
      
      // Get the information related to all public members of 'MyClass'.
      myMemberInfo = myType->GetMembers();
      Console::WriteLine( "\nThe members of class '{0}' are :\n", myType );
      for ( int i = 0; i < myMemberInfo->Length; i++ )
      {
         
         // Display name and type of the concerned member.
         Console::WriteLine( "'{0}' is a {1}", myMemberInfo[ i ]->Name, myMemberInfo[ i ]->MemberType );

      }
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "Exception : {0}", e->Message );
   }

}
class MyClass
{
   public int myInt = 0;
   public string myString = null;

   public MyClass()
   {
   }
   public void Myfunction()
   {
   }
}

class Type_GetMembers
{
   public static void Main()
   {
      try
      {
         MyClass myObject = new MyClass();
         MemberInfo[] myMemberInfo;

         // Get the type of 'MyClass'.
         Type myType = myObject.GetType();

         // Get the information related to all public member's of 'MyClass'.
         myMemberInfo = myType.GetMembers();

         Console.WriteLine( "\nThe members of class '{0}' are :\n", myType);
         for (int i =0 ; i < myMemberInfo.Length ; i++)
         {
            // Display name and type of the concerned member.
            Console.WriteLine( "'{0}' is a {1}", myMemberInfo[i].Name, myMemberInfo[i].MemberType);
         }
      }
      catch(SecurityException e)
      {
         Console.WriteLine("Exception : " + e.Message );
      }
   }
}
type MyClass =
    val public myInt: int
    val public myString: string

    new () = { myInt = 0; myString = null}

    member _.MyMethod() = ()

try
    let myObject = MyClass()

    // Get the type of 'MyClass'.
    let myType = myObject.GetType()

    // Get the information related to all public member's of 'MyClass'.
    let myMemberInfo = myType.GetMembers()

    printfn $"\nThe members of class '{myType}' are :\n"
    for i = 0 to myMemberInfo.Length - 1 do
    // Display name and type of the concerned member.
        printfn $"'{myMemberInfo[i].Name}' is a {myMemberInfo[i].MemberType}"
with e ->
    printfn $"Exception : {e.Message}"
Class [MyClass]
   Public myInt As Integer = 0
   Public myString As String = Nothing
   
   
   Public Sub New()
   End Sub
   
   Public Sub Myfunction()
   End Sub
End Class

Class Type_GetMembers
   
   Public Shared Sub Main()
      Try
         Dim myObject As New [MyClass]()
         Dim myMemberInfo() As MemberInfo
         
         ' Get the type of 'MyClass'.
         Dim myType As Type = myObject.GetType()
         
         ' Get the information related to all public member's of 'MyClass'. 
         myMemberInfo = myType.GetMembers()
         
         Console.WriteLine(ControlChars.Cr + "The members of class '{0}' are :" + ControlChars.Cr, myType)
         Dim i As Integer
         For i = 0 To myMemberInfo.Length - 1
            ' Display name and type of the concerned member.
            Console.WriteLine("'{0}' is a {1}", myMemberInfo(i).Name, myMemberInfo(i).MemberType)
         Next i

      Catch e As SecurityException
         Console.WriteLine(("Exception : " + e.Message.ToString()))
      End Try
   End Sub
End Class

설명

멤버에는 속성, 메서드, 생성자, 필드, 이벤트 및 중첩 형식이 포함됩니다.

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

이 메서드 오버로드는 를 사용하여 메서드 오버로드 | | BindingFlags.PublicBindingFlags.StaticBindingFlags.Instance를 호출 GetMembers(BindingFlags) 합니다(BindingFlags.StaticBindingFlags.PublicOrBindingFlags.InstanceOr Visual Basic의 경우). 클래스 이니셜라이저(정적 생성자)를 찾을 수 없습니다. 클래스 이니셜라이저를 찾으려면 오버로드를 GetMembers(BindingFlags) 호출하고 (BindingFlags.StaticOrBindingFlags.NonPublic Visual Basic에서)를 지정 BindingFlags.Static | BindingFlags.NonPublic 합니다. 속성을 사용하여 클래스 이니셜라이저를 TypeInitializer 가져올 수도 있습니다.

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

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

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

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

현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 형식 매개 변수가 적절한 형식 인수로 대체된 개체를 반환 MemberInfo 합니다.

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

추가 정보

적용 대상