Type.GetMethods 메서드

정의

현재 Type의 메서드를 가져옵니다.

오버로드

GetMethods(BindingFlags)

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

GetMethods()

현재 Type의 모든 public 메서드를 반환합니다.

GetMethods(BindingFlags)

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

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

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

매개 변수

bindingAttr
BindingFlags

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

또는

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

반환

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

또는

현재 MethodInfo에 대해 정의된 메서드가 없거나 정의된 필드 중 해당 바인딩 제약 조건과 일치하는 메서드가 없을 경우 Type 형식의 빈 배열입니다.

구현

예제

다음 예제에서는 두 개의 public 메서드와 하나의 보호된 메서드를 사용하여 클래스를 만들고, 에 해당하는 개체를 TypeMyTypeClass만들고, 모든 public 및 non-public 메서드를 가져오고, 해당 이름을 표시합니다.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

// Create a class having two public methods and one protected method.
public ref class MyTypeClass
{
public:
   void MyMethods(){}

   int MyMethods1()
   {
      return 3;
   }


protected:
   String^ MyMethods2()
   {
      return "hello";
   }
};

void DisplayMethodInfo( array<MethodInfo^>^myArrayMethodInfo )
{
   // Display information for all methods.
   for ( int i = 0; i < myArrayMethodInfo->Length; i++ )
   {
      MethodInfo^ myMethodInfo = dynamic_cast<MethodInfo^>(myArrayMethodInfo[ i ]);
      Console::WriteLine( "\nThe name of the method is {0}.", myMethodInfo->Name );
   }
}

int main()
{
   Type^ myType = MyTypeClass::typeid;
   
   // Get the public methods.
   array<MethodInfo^>^myArrayMethodInfo = myType->GetMethods( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance | BindingFlags::DeclaredOnly) );
   Console::WriteLine( "\nThe number of public methods is {0}->", myArrayMethodInfo->Length );
   
   // Display all the methods.
   DisplayMethodInfo( myArrayMethodInfo );
   
   // Get the nonpublic methods.
   array<MethodInfo^>^myArrayMethodInfo1 = myType->GetMethods( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance | BindingFlags::DeclaredOnly) );
   Console::WriteLine( "\nThe number of protected methods is {0}->", myArrayMethodInfo1->Length );
   
   // Display information for all methods.
   DisplayMethodInfo( myArrayMethodInfo1 );
}
using System;
using System.Reflection;
using System.Reflection.Emit;

// Create a class having two public methods and one protected method.
public class MyTypeClass
{
    public void MyMethods()
    {
    }
    public int MyMethods1()
    {
        return 3;
    }
    protected String MyMethods2()
    {
        return "hello";
    }
}
public class TypeMain
{
    public static void Main()
    {
        Type myType =(typeof(MyTypeClass));
        // Get the public methods.
        MethodInfo[] myArrayMethodInfo = myType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);
        Console.WriteLine("\nThe number of public methods is {0}.", myArrayMethodInfo.Length);
        // Display all the methods.
        DisplayMethodInfo(myArrayMethodInfo);
        // Get the nonpublic methods.
        MethodInfo[] myArrayMethodInfo1 = myType.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);
        Console.WriteLine("\nThe number of protected methods is {0}.", myArrayMethodInfo1.Length);
        // Display information for all methods.
        DisplayMethodInfo(myArrayMethodInfo1);		
    }
    public static void DisplayMethodInfo(MethodInfo[] myArrayMethodInfo)
    {
        // Display information for all methods.
        for(int i=0;i<myArrayMethodInfo.Length;i++)
        {
            MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i];
            Console.WriteLine("\nThe name of the method is {0}.", myMethodInfo.Name);
        }
    }
}

Imports System.Reflection
Imports System.Reflection.Emit

' Create a class having two public methods and one protected method.
Public Class MyTypeClass
    Public Sub MyMethods()
    End Sub
    Public Function MyMethods1() As Integer
        Return 3
    End Function 'MyMethods1
    Protected Function MyMethods2() As [String]
        Return "hello"
    End Function 'MyMethods2
End Class
Public Class TypeMain
    Public Shared Sub Main()

        Dim myType As Type = GetType(MyTypeClass)
        ' Get the public methods.
        Dim myArrayMethodInfo As MethodInfo() = myType.GetMethods((BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.DeclaredOnly))
        Console.WriteLine((ControlChars.Cr + "The number of public methods is " & myArrayMethodInfo.Length.ToString() & "."))
        ' Display all the public methods.
        DisplayMethodInfo(myArrayMethodInfo)
        ' Get the nonpublic methods.
        Dim myArrayMethodInfo1 As MethodInfo() = myType.GetMethods((BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.DeclaredOnly))
        Console.WriteLine((ControlChars.Cr + "The number of protected methods is " & myArrayMethodInfo1.Length.ToString() & "."))
        ' Display all the nonpublic methods.
        DisplayMethodInfo(myArrayMethodInfo1)
    End Sub

    Public Shared Sub DisplayMethodInfo(ByVal myArrayMethodInfo() As MethodInfo)
        ' Display information for all methods.
        Dim i As Integer
        For i = 0 To myArrayMethodInfo.Length - 1
            Dim myMethodInfo As MethodInfo = CType(myArrayMethodInfo(i), MethodInfo)
            Console.WriteLine((ControlChars.Cr + "The name of the method is " & myMethodInfo.Name & "."))
        Next i
    End Sub
End Class

설명

오버로드가 GetMethods(BindingFlags) 메서드 정보를 bindingAttr 성공적으로 검색하려면 인수에 및 중 하나 BindingFlags.Instance 이상과 BindingFlags.StaticBindingFlags.PublicBindingFlags.NonPublic 하나 이상이 포함되어야 합니다.

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

  • 인스턴스 메서드를 포함하도록 지정 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 및 이전 버전에서는 메서드가 GetMethods 사전순 또는 선언 순서와 같은 특정 순서로 메서드를 반환하지 않습니다. 코드는 해당 순서가 다르기 때문에 메서드가 반환되는 순서에 의존해서는 안 됩니다. 그러나 .NET 7부터는 어셈블리의 메타데이터 순서에 따라 순서가 결정적입니다.

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

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

추가 정보

적용 대상

GetMethods()

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

현재 Type의 모든 public 메서드를 반환합니다.

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

반환

현재 MethodInfo에 대해 정의된 모든 public 메서드를 나타내는 Type 개체의 배열입니다.

또는

현재 MethodInfo에 대해 정의된 public 메서드가 없는 경우 Type 형식의 빈 배열입니다.

구현

설명

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

생성자는 이 호출에서 반환되는 메서드 배열에 포함되지 않습니다. 를 별도로 호출하여 GetConstructors() 생성자 메서드를 가져옵니다.

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

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

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

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

참고

생성자 및 메서드를 조회할 때 매개 변수를 생략할 수 없습니다. 호출할 때만 매개 변수를 생략할 수 있습니다.

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

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

추가 정보

적용 대상