Type.GetMethod 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
현재 Type.의 특정 메서드를 가져옵니다.
오버로드
| Name | Description |
|---|---|
| GetMethod(String, Int32, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) |
지정된 바인딩 제약 조건 및 지정된 호출 규칙을 사용하여 매개 변수가 지정된 제네릭 매개 변수 수, 인수 형식 및 한정자와 일치하는 지정된 메서드를 검색합니다. |
| GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) |
지정된 바인딩 제약 조건 및 지정된 호출 규칙을 사용하여 매개 변수가 지정된 인수 형식 및 한정자와 일치하는 지정된 메서드를 검색합니다. |
| GetMethod(String, Int32, BindingFlags, Binder, Type[], ParameterModifier[]) |
지정된 바인딩 제약 조건을 사용하여 매개 변수가 지정된 제네릭 매개 변수 수, 인수 형식 및 한정자와 일치하는 지정된 메서드를 검색합니다. |
| GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[]) |
지정된 바인딩 제약 조건을 사용하여 매개 변수가 지정된 인수 형식 및 한정자와 일치하는 지정된 메서드를 검색합니다. |
| GetMethod(String, Int32, Type[], ParameterModifier[]) |
매개 변수가 지정된 제네릭 매개 변수 수, 인수 형식 및 한정자와 일치하는 지정된 public 메서드를 검색합니다. |
| GetMethod(String, Int32, BindingFlags, Type[]) |
지정된 바인딩 제약 조건을 사용하여 매개 변수가 지정된 제네릭 매개 변수 수 및 인수 형식과 일치하는 지정된 메서드를 검색합니다. |
| GetMethod(String, BindingFlags) |
지정된 바인딩 제약 조건을 사용하여 지정된 메서드를 검색합니다. |
| GetMethod(String, BindingFlags, Type[]) |
지정된 바인딩 제약 조건을 사용하여 매개 변수가 지정된 인수 형식과 일치하는 지정된 메서드를 검색합니다. |
| GetMethod(String, Int32, Type[]) |
매개 변수가 지정된 제네릭 매개 변수 수 및 인수 형식과 일치하는 지정된 public 메서드를 검색합니다. |
| GetMethod(String, Type[]) |
매개 변수가 지정된 인수 형식과 일치하는 지정된 public 메서드를 검색합니다. |
| GetMethod(String) |
지정된 이름의 public 메서드를 검색합니다. |
| GetMethod(String, Type[], ParameterModifier[]) |
매개 변수가 지정된 인수 형식 및 한정자와 일치하는 지정된 public 메서드를 검색합니다. |
GetMethod(String, Int32, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
지정된 바인딩 제약 조건 및 지정된 호출 규칙을 사용하여 매개 변수가 지정된 제네릭 매개 변수 수, 인수 형식 및 한정자와 일치하는 지정된 메서드를 검색합니다.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, bindingAttr As BindingFlags, binder As Binder, callConvention As CallingConventions, types As Type(), modifiers As ParameterModifier()) As MethodInfo
매개 변수
- name
- String
가져올 public 메서드의 이름을 포함하는 문자열입니다.
- genericParameterCount
- Int32
메서드의 제네릭 형식 매개 변수 수입니다.
- binder
- Binder
속성 집합을 정의하고 바인딩을 사용하도록 설정하는 개체로, 오버로드된 메서드 선택, 인수 형식 강제 변환 및 리플렉션을 통해 멤버 호출을 포함할 수 있습니다.
-또는-
를 사용할 null 참조(Nothing Visual Basic의 경우)입니다 DefaultBinder.
- callConvention
- CallingConventions
인수의 순서 및 레이아웃, 반환 값 전달 방법, 인수에 사용되는 레지스터 및 스택 정리 방법과 관련하여 사용할 규칙 집합을 지정하는 개체입니다.
- types
- Type[]
가져올 메서드에 Type 대한 매개 변수의 수, 순서 및 형식을 나타내는 개체의 배열입니다.
-또는-
매개 변수를 사용하지 않는 메서드를 가져오는 빈 개체 배열 Type (필드에서 제공 EmptyTypes )입니다.
- modifiers
- ParameterModifier[]
배열의 ParameterModifier 해당 요소와 연결된 특성을 나타내는 개체의 types 배열입니다. COM interop을 통해 호출할 때만 사용되며 참조로 전달되는 매개 변수만 처리됩니다. 기본 바인더는 이 매개 변수를 처리하지 않습니다.
반환
지정된 제네릭 매개 변수 수, 인수 형식, 한정자, 바인딩 제약 조건 및 호출 규칙(있는 경우)과 일치하는 메서드를 나타내는 개체입니다. 그렇지 않으면 . null
- 특성
예외
genericParameterCount 가 음수입니다.
적용 대상
GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
지정된 바인딩 제약 조건 및 지정된 호출 규칙을 사용하여 매개 변수가 지정된 인수 형식 및 한정자와 일치하는 지정된 메서드를 검색합니다.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
member this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, binder As Binder, callConvention As CallingConventions, types As Type(), modifiers As ParameterModifier()) As MethodInfo
매개 변수
- name
- String
가져올 메서드의 이름을 포함하는 문자열입니다.
- binder
- Binder
속성 집합을 정의하고 바인딩을 사용하도록 설정하는 개체로, 오버로드된 메서드 선택, 인수 형식 강제 변환 및 리플렉션을 통해 멤버 호출을 포함할 수 있습니다.
-또는-
를 사용할 null 참조(Nothing Visual Basic의 경우)입니다 DefaultBinder.
- callConvention
- CallingConventions
인수의 순서 및 레이아웃, 반환 값 전달 방법, 인수에 사용되는 레지스터 및 스택 정리 방법과 관련하여 사용할 규칙 집합을 지정하는 개체입니다.
- types
- Type[]
가져올 메서드에 Type 대한 매개 변수의 수, 순서 및 형식을 나타내는 개체의 배열입니다.
-또는-
매개 변수를 사용하지 않는 메서드를 가져오는 빈 개체 배열 Type (필드에서 제공 EmptyTypes )입니다.
- modifiers
- ParameterModifier[]
배열의 ParameterModifier 해당 요소와 연결된 특성을 나타내는 개체의 types 배열입니다. COM interop을 통해 호출할 때만 사용되며 참조로 전달되는 매개 변수만 처리됩니다. 기본 바인더는 이 매개 변수를 처리하지 않습니다.
반환
지정된 요구 사항과 일치하는 메서드를 나타내는 개체입니다(있는 경우). 그렇지 않으면 . null
구현
- 특성
예외
지정된 이름과 지정된 바인딩 제약 조건과 일치하는 메서드가 두 개 이상 있습니다.
예제
다음 예제에서는 바인딩 제약 조건, 호출 규칙 및 다양한 인수 형식을 지정하는 특정 오버로드 MethodA를 찾습니다.
메모
Visual C# 2005 예제에는 /unsafe 컴파일러 옵션이 필요합니다.
using System;
using System.Reflection;
class Program3
{
// Methods to get:
public void MethodA(int i, int j) { }
public void MethodA(int[] i) { }
public unsafe void MethodA(int* i) { }
public void MethodA(ref int r) {}
// Method that takes an out parameter:
public void MethodA(int i, out int o) { o = 100;}
static void Main(string[] args)
{
MethodInfo mInfo;
// Get MethodA(int i, int j)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
CallingConventions.Any,
new Type[] { typeof(int), typeof(int) },
null);
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(int[] i)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
CallingConventions.Any,
new Type[] { typeof(int[]) },
null);
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(int* i)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
CallingConventions.Any,
new Type[] { typeof(int).MakePointerType() },
null);
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(ref int r)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
CallingConventions.Any,
new Type[] { typeof(int).MakeByRefType() },
null);
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(int i, out int o)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
CallingConventions.Any,
new Type[] { typeof(int), typeof(int).MakeByRefType() },
null);
Console.WriteLine("Found method: {0}", mInfo);
}
}
open System.Reflection
type Program() =
// Methods to get:
member _.MethodA(i: int, j: int) = ()
member _.MethodA(i: int[]) = ()
member _.MethodA(i: int nativeptr) = ()
member _.MethodA(r: int byref) = ()
// Method that takes an outref parameter:
member _.MethodA(i: int, o: int outref) = o <- 100
do
// Get MethodA(int i, int j)
let mInfo =
typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>; typeof<int> |], null)
printfn $"Found method: {mInfo}"
// Get MethodA(int[] i)
let mInfo =
typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int[]> |], null)
printfn $"Found method: {mInfo}"
// Get MethodA(int* i)
let mInfo =
typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>.MakePointerType() |], null)
printfn $"Found method: {mInfo}"
// Get MethodA(ref int r)
let mInfo =
typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>.MakeByRefType() |], null)
printfn $"Found method: {mInfo}"
// Get MethodA(int i, out int o)
let mInfo =
typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>; typeof<int>.MakeByRefType() |], null)
printfn $"Found method: {mInfo}"
Imports System.Reflection
Imports System.Runtime.InteropServices
Class Program
' Methods to get:
Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)
End Sub
Public Overloads Sub MethodA(ByVal i() As Integer)
End Sub
Public Overloads Sub MethodA(ByRef r As Integer)
End Sub
' Method that takes an integer and an out parameter. Note that an
' Imports reference is needed to System.Runtime.InteropServices
' for the <OutAttribute>, which can be shortened to <Out>.
Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
o = 100
End Sub
Public Shared Sub Main(ByVal args() As String)
Dim mInfo As MethodInfo
' Get MethodA(ByVal i As Integer, ByVal l As Long)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
CallingConventions.Any, _
New Type() {GetType(System.Int32), _
GetType(System.Int64)}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByVal i() As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
CallingConventions.Any, _
New Type() {GetType(System.Int32())}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByRef r As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
CallingConventions.Any, _
New Type() {GetType(System.Int32).MakeByRefType}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
CallingConventions.Any, _
New Type() {GetType(System.Int32), GetType(System.Int32).MakeByRefType}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
End Sub
End Class
설명
기본 바인더는 ParameterModifier를 처리하지 않지만 (modifiers 매개 변수) 추상 System.Reflection.Binder 클래스를 사용하여 modifiers를 처리하는 사용자 지정 바인더를 작성할 수 있습니다.
ParameterModifier 는 COM interop을 통해 호출할 때만 사용되며 참조로 전달되는 매개 변수만 처리됩니다.
다음 표에서는 형식을 반영할 때 메서드에서 반환되는 기본 클래스의 GetXXX 멤버를 보여 줍니다.
| 멤버 형식 | 정적 | 비정적 |
|---|---|---|
| 생성자 | No | No |
| 분야 | No | 예. 필드는 항상 이름과 서명을 기준으로 숨겨집니다. |
| 이벤트 | 해당 없음 | 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 이름별 숨기기 및 서명으로 처리합니다. 아래 참고 2를 참조하세요. |
| 메서드 | No | 예. 메서드(가상 및 가상이 아닌 메서드)는 이름으로 숨기기 또는 이름과 서명으로 숨기기가 가능합니다. |
| 중첩 타입 | No | No |
| 재산 | 해당 없음 | 일반적인 형식 시스템 규칙은 상속이 속성을 구현하는 메서드의 상속과 동일하다는 것입니다. 리플렉션은 속성을 이름별 숨기기 및 서명으로 처리합니다. 아래 참고 2를 참조하세요. |
이름별 및 서명에 의한 숨기기는 사용자 지정 한정자, 반환 형식, 매개 변수 형식, 센티넬 및 비관리 호출 규칙을 포함하여 서명의 모든 부분을 고려합니다. 이진 비교입니다.
리플렉션의 경우 속성과 이벤트는 이름 및 서명별로 숨겨집니다. 기본 클래스에 get 및 set 접근자가 모두 있는 속성이 있지만 파생 클래스에 get 접근자만 있는 경우 파생 클래스 속성은 기본 클래스 속성을 숨기며 기본 클래스의 setter에 액세스할 수 없습니다.
사용자 지정 특성은 공용 형식 시스템의 일부가 아닙니다.
다음 BindingFlags 필터 플래그를 사용하여 검색에 포함할 메서드를 정의할 수 있습니다.
반환을 얻으려면
BindingFlags.Instance또는BindingFlags.Static중 하나를 지정해야 합니다.검색에 공용 메서드를 포함하도록 지정
BindingFlags.Public합니다.검색에 비공개 메서드(즉, private, internal 및 protected 메서드)를 포함하도록 지정
BindingFlags.NonPublic합니다.위계에서
BindingFlags.FlattenHierarchy및public정적 멤버를 포함하도록protected을(를) 지정하세요. 상속된 클래스에서는private정적 멤버가 포함되지 않습니다.
다음 BindingFlags 한정자 플래그를 사용하여 검색 작동 방식을 변경할 수 있습니다.
BindingFlags.IgnoreCase의 대소문자를 무시합니다.BindingFlags.DeclaredOnly단순히 상속된 메서드가 아니라 선언 Type된 메서드만 검색합니다.
자세한 내용은 System.Reflection.BindingFlags을 참조하세요.
메모
생성자와 메서드를 조회할 때는 매개 변수를 생략할 수 없습니다. 호출할 때만 매개 변수를 생략할 수 있습니다.
현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 적절한 형식 인수로 대체된 형식 매개 변수를 반환 MethodInfo 합니다.
현재 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 메서드 또는 클래스 제약 조건이 없는 경우의 Object 메서드를 검색합니다.
메모
제네릭 메서드의 경우 형식 인수 name를 포함하지 마세요. 예를 들어 C# 코드 GetMember("MyMethod<int>") 는 형식int의 제네릭 인수가 하나 있는 메서드가 아니라 텍스트 이름이 "MyMethod<int>"인 MyMethod 멤버를 검색합니다.
추가 정보
- MethodInfo
- String
- BindingFlags
- Binder
- DefaultBinder
- CallingConventions
- ParameterModifier
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
적용 대상
GetMethod(String, Int32, BindingFlags, Binder, Type[], ParameterModifier[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
지정된 바인딩 제약 조건을 사용하여 매개 변수가 지정된 제네릭 매개 변수 수, 인수 형식 및 한정자와 일치하는 지정된 메서드를 검색합니다.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, bindingAttr As BindingFlags, binder As Binder, types As Type(), modifiers As ParameterModifier()) As MethodInfo
매개 변수
- name
- String
가져올 public 메서드의 이름을 포함하는 문자열입니다.
- genericParameterCount
- Int32
메서드의 제네릭 형식 매개 변수 수입니다.
- binder
- Binder
속성 집합을 정의하고 바인딩을 사용하도록 설정하는 개체로, 오버로드된 메서드 선택, 인수 형식 강제 변환 및 리플렉션을 통해 멤버 호출을 포함할 수 있습니다.
-또는-
를 사용할 null 참조(Nothing Visual Basic의 경우)입니다 DefaultBinder.
- types
- Type[]
가져올 메서드에 Type 대한 매개 변수의 수, 순서 및 형식을 나타내는 개체의 배열입니다.
-또는-
매개 변수를 사용하지 않는 메서드를 가져오는 빈 개체 배열 Type (필드에서 제공 EmptyTypes )입니다.
- modifiers
- ParameterModifier[]
배열의 ParameterModifier 해당 요소와 연결된 특성을 나타내는 개체의 types 배열입니다. COM interop을 통해 호출할 때만 사용되며 참조로 전달되는 매개 변수만 처리됩니다. 기본 바인더는 이 매개 변수를 처리하지 않습니다.
반환
지정된 제네릭 매개 변수 수, 인수 형식, 한정자 및 바인딩 제약 조건(있는 경우)과 일치하는 메서드를 나타내는 개체입니다. 그렇지 않으면 . null
- 특성
예외
genericParameterCount 가 음수입니다.
적용 대상
GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
지정된 바인딩 제약 조건을 사용하여 매개 변수가 지정된 인수 형식 및 한정자와 일치하는 지정된 메서드를 검색합니다.
public:
virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
abstract member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, binder As Binder, types As Type(), modifiers As ParameterModifier()) As MethodInfo
매개 변수
- name
- String
가져올 메서드의 이름을 포함하는 문자열입니다.
- binder
- Binder
속성 집합을 정의하고 바인딩을 사용하도록 설정하는 개체로, 오버로드된 메서드 선택, 인수 형식 강제 변환 및 리플렉션을 통해 멤버 호출을 포함할 수 있습니다.
-또는-
를 사용할 null 참조(Nothing Visual Basic의 경우)입니다 DefaultBinder.
- types
- Type[]
가져올 메서드에 Type 대한 매개 변수의 수, 순서 및 형식을 나타내는 개체의 배열입니다.
-또는-
매개 변수를 사용하지 않는 메서드를 가져오는 빈 개체 배열 Type (필드에서 제공 EmptyTypes )입니다.
- modifiers
- ParameterModifier[]
배열의 ParameterModifier 해당 요소와 연결된 특성을 나타내는 개체의 types 배열입니다. COM interop을 통해 호출할 때만 사용되며 참조로 전달되는 매개 변수만 처리됩니다. 기본 바인더는 이 매개 변수를 처리하지 않습니다.
반환
지정된 요구 사항과 일치하는 메서드를 나타내는 개체입니다(있는 경우). 그렇지 않으면 . null
구현
- 특성
예외
지정된 이름과 지정된 바인딩 제약 조건과 일치하는 메서드가 두 개 이상 있습니다.
예제
다음 예제에서는 바인딩 제약 조건 MethodA및 다양한 인수 형식을 지정하는 특정 오버로드를 찾습니다.
메모
Visual C# 2005 예제에는 /unsafe 컴파일러 옵션이 필요합니다.
using System;
using System.Reflection;
class Program5
{
// Methods to get:
public void MethodA(int i, int j) { }
public void MethodA(int[] i) { }
public unsafe void MethodA(int* i) { }
public void MethodA(ref int r) {}
// Method that takes an out parameter.
public void MethodA(int i, out int o) { o = 100; }
static void Main(string[] args)
{
MethodInfo mInfo;
// Get MethodA(int i, int j)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
new Type[] { typeof(int), typeof(int) },
null);
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(int[] i)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
new Type[] { typeof(int[]) },
null);
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(int* i)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
new Type[] { typeof(int).MakePointerType() },
null);
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(ref int r)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
new Type[] { typeof(int).MakeByRefType() },
null);
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(int i, out int o)
mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance,
null,
new Type[] { typeof(int), typeof(int).MakeByRefType() },
null);
Console.WriteLine("Found method: {0}", mInfo);
}
}
open System
open System.Reflection
type Program() =
// Methods to get:
member _.MethodA(i: int, j: int) = ()
member _.MethodA(i: int[]) = ()
member _.MethodA(i: int nativeptr) = ()
member _.MethodA(r: int byref) = ()
// Method that takes an outref parameter:
member _.MethodA(i: int, o: int outref) = o <- 100
do
// Get MethodA(int i, int j)
let mInfo =
typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>; typeof<int> |], null)
printfn $"Found method: {mInfo}"
// Get MethodA(int[] i)
let mInfo =
typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int[]> |], null)
printfn $"Found method: {mInfo}"
// Get MethodA(int* i)
let mInfo =
typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>.MakePointerType() |], null)
printfn $"Found method: {mInfo}"
// Get MethodA(ref int r)
let mInfo =
typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>.MakeByRefType() |], null)
printfn $"Found method: {mInfo}"
// Get MethodA(int i, out int o)
let mInfo =
typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>; typeof<int>.MakeByRefType() |], null)
printfn $"Found method: {mInfo}"
Imports System.Reflection
Imports System.Runtime.InteropServices
Class Program
' Methods to get:
Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)
End Sub
Public Overloads Sub MethodA(ByVal i() As Integer)
End Sub
Public Overloads Sub MethodA(ByRef r As Integer)
End Sub
' Method that takes an out parameter. Note that an Imports
' reference is needed to System.Runtime.InteropServices
' for the <OutAttribute>, which can be shortened to <Out>.
Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
o = 100
End Sub
Public Shared Sub Main(ByVal args() As String)
Dim mInfo As MethodInfo
' Get MethodA(ByVal i As Integer, ByVal l As Long)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
New Type() {GetType(System.Int32), _
GetType(System.Int64)}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByVal i() As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
New Type() {GetType(System.Int32())}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByRef r As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
New Type() {GetType(System.Int32).MakeByRefType}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
mInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance, _
Nothing, _
New Type() {GetType(System.Int32), GetType(System.Int32).MakeByRefType}, _
Nothing)
Console.WriteLine("Found method: {0}", mInfo)
End Sub
End Class
설명
기본 바인더는 ParameterModifier를 처리하지 않지만 (modifiers 매개 변수) 추상 System.Reflection.Binder 클래스를 사용하여 modifiers를 처리하는 사용자 지정 바인더를 작성할 수 있습니다.
ParameterModifier 는 COM interop을 통해 호출할 때만 사용되며 참조로 전달되는 매개 변수만 처리됩니다.
다음 BindingFlags 필터 플래그를 사용하여 검색에 포함할 메서드를 정의할 수 있습니다.
반환을 얻으려면
BindingFlags.Instance또는BindingFlags.Static중 하나를 지정해야 합니다.검색에 공용 메서드를 포함하도록 지정
BindingFlags.Public합니다.검색에 비공개 메서드(즉, private, internal 및 protected 메서드)를 포함하도록 지정
BindingFlags.NonPublic합니다.위계에서
BindingFlags.FlattenHierarchy및public정적 멤버를 포함하도록protected을(를) 지정하세요. 상속된 클래스에서는private정적 멤버가 포함되지 않습니다.
다음 BindingFlags 한정자 플래그를 사용하여 검색 작동 방식을 변경할 수 있습니다.
BindingFlags.IgnoreCase의 대소문자를 무시합니다.BindingFlags.DeclaredOnly단순히 상속된 메서드가 아니라 선언 Type된 메서드만 검색합니다.
자세한 내용은 System.Reflection.BindingFlags을 참조하세요.
메모
생성자와 메서드를 조회할 때는 매개 변수를 생략할 수 없습니다. 호출할 때만 매개 변수를 생략할 수 있습니다.
현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 적절한 형식 인수로 대체된 형식 매개 변수를 반환 MethodInfo 합니다.
현재 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 메서드 또는 클래스 제약 조건이 없는 경우의 Object 메서드를 검색합니다.
메모
제네릭 메서드의 경우 형식 인수 name를 포함하지 마세요. 예를 들어 C# 코드 GetMember("MyMethod<int>") 는 형식int의 제네릭 인수가 하나 있는 메서드가 아니라 텍스트 이름이 "MyMethod<int>"인 MyMethod 멤버를 검색합니다.
추가 정보
- MethodInfo
- String
- BindingFlags
- Binder
- DefaultBinder
- ParameterModifier
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
적용 대상
GetMethod(String, Int32, Type[], ParameterModifier[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
매개 변수가 지정된 제네릭 매개 변수 수, 인수 형식 및 한정자와 일치하는 지정된 public 메서드를 검색합니다.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, int genericParameterCount, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
member this.GetMethod : string * int * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, types As Type(), modifiers As ParameterModifier()) As MethodInfo
매개 변수
- name
- String
가져올 public 메서드의 이름을 포함하는 문자열입니다.
- genericParameterCount
- Int32
메서드의 제네릭 형식 매개 변수 수입니다.
- types
- Type[]
가져올 메서드에 Type 대한 매개 변수의 수, 순서 및 형식을 나타내는 개체의 배열입니다.
-또는-
매개 변수를 사용하지 않는 메서드를 가져오는 빈 개체 배열 Type (필드에서 제공 EmptyTypes )입니다.
- modifiers
- ParameterModifier[]
배열의 ParameterModifier 해당 요소와 연결된 특성을 나타내는 개체의 types 배열입니다. COM interop을 통해 호출할 때만 사용되며 참조로 전달되는 매개 변수만 처리됩니다. 기본 바인더는 이 매개 변수를 처리하지 않습니다.
반환
지정된 제네릭 매개 변수 수, 인수 형식 및 한정자와 일치하는 public 메서드를 나타내는 개체입니다(있는 경우). 그렇지 않으면 . null
- 특성
예외
genericParameterCount 가 음수입니다.
적용 대상
GetMethod(String, Int32, BindingFlags, Type[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
지정된 바인딩 제약 조건을 사용하여 매개 변수가 지정된 제네릭 매개 변수 수 및 인수 형식과 일치하는 지정된 메서드를 검색합니다.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * System.Reflection.BindingFlags * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, bindingAttr As BindingFlags, types As Type()) As MethodInfo
매개 변수
- name
- String
가져올 메서드의 이름을 포함하는 문자열입니다.
- genericParameterCount
- Int32
메서드의 제네릭 형식 매개 변수 수입니다.
- types
- Type[]
가져올 메서드에 Type 대한 매개 변수의 수, 순서 및 형식을 나타내는 개체의 배열입니다.
-또는-
매개 변수를 사용하지 않는 메서드를 가져오는 빈 개체 배열 Type (필드에서 제공 EmptyTypes )입니다.
반환
지정된 제네릭 매개 변수 수, 인수 형식 및 바인딩 제약 조건(있는 경우)과 일치하는 메서드를 나타내는 개체입니다. 그렇지 않으면 . null
- 특성
적용 대상
GetMethod(String, BindingFlags)
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
지정된 바인딩 제약 조건을 사용하여 지정된 메서드를 검색합니다.
public:
virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.MethodInfo GetMethod(string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
abstract member GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
abstract member GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags) As MethodInfo
매개 변수
- name
- String
가져올 메서드의 이름을 포함하는 문자열입니다.
반환
지정된 요구 사항과 일치하는 메서드를 나타내는 개체입니다(있는 경우). 그렇지 않으면 . null
구현
- 특성
예외
지정된 이름과 지정된 바인딩 제약 조건과 일치하는 메서드가 두 개 이상 있습니다.
name은 null입니다.
예제
다음 예제에서는 지정된 바인딩 플래그와 일치하는 메서드를 가져옵니다.
using System;
using System.Reflection;
class Program2
{
// Method to get:
public void MethodA() { }
static void Main(string[] args)
{
// Get MethodA()
MethodInfo mInfo = typeof(Program).GetMethod("MethodA",
BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine("Found method: {0}", mInfo);
}
}
open System.Reflection
type Program() =
// Method to get:
member _.MethodA() = ()
// Get MethodA()
let mInfo = typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance)
printfn $"Found method: {mInfo}"
Imports System.Reflection
Class Program
' Method to get:
Public Sub MethodA()
End Sub
Public Shared Sub Main(ByVal args() As String)
' Get MethodA()
Dim mInfo As MethodInfo = GetType(Program).GetMethod("MethodA", _
BindingFlags.Public Or BindingFlags.Instance)
Console.WriteLine("Found method: {0}", mInfo)
End Sub
End Class
설명
다음 BindingFlags 필터 플래그를 사용하여 검색에 포함할 메서드를 정의할 수 있습니다.
반환을 얻으려면 BindingFlags.Instance 또는 BindingFlags.Static 중 하나를 지정해야 합니다.
검색에 공용 메서드를 포함하도록 지정 BindingFlags.Public 합니다.
비공용 메서드(즉, 프라이빗, 내부 및 보호된 메서드)를 검색에 포함하도록 지정 BindingFlags.NonPublic 합니다.
위계에서 BindingFlags.FlattenHierarchy 및
public정적 멤버를 포함하도록protected을(를) 지정하세요. 상속된 클래스에서는private정적 멤버가 포함되지 않습니다.
다음 BindingFlags 한정자 플래그를 사용하여 검색 작동 방식을 변경할 수 있습니다.
BindingFlags.IgnoreCase의 대소문자를 무시합니다.
BindingFlags.DeclaredOnly 단순히 상속된 메서드가 아니라 선언 Type된 메서드만 검색합니다.
자세한 내용은 System.Reflection.BindingFlags을 참조하세요.
메서드가 오버로드되고 둘 이상의 오버로드가 인수로 bindingAttr 지정된 제약 조건을 충족하는 경우 메서드는 예외를 AmbiguousMatchException throw합니다. 다음 예제에서는 다음과 같은 이유로 예외가 throw됩니다.
형식에는
TestClass메서드DisplayValue(String)의DisplayValue두 개의 공용 인스턴스 오버로드와DisplayValue(String, Object[]).형식에는
TestClass메서드의 두 개의 공용 인스턴스 오버로드가Equals있으며, 그 중 하나는 다음Equals(TestClass)Equals(Object)에서 Object상속됩니다.
using System;
using System.Reflection;
public class TestClass
{
public void DisplayValue(String s)
{
Console.WriteLine(s);
}
public void DisplayValue(String s, params Object[] values)
{
Console.WriteLine(s, values);
}
public static bool Equals(TestClass t1, TestClass t2)
{
return Object.ReferenceEquals(t1, t2);
}
public bool Equals(TestClass t)
{
return Object.ReferenceEquals(this, t);
}
}
public class Example1
{
public static void Main()
{
Type t = typeof(TestClass);
RetrieveMethod(t, "DisplayValue", BindingFlags.Public | BindingFlags.Instance);
RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Instance);
RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Static);
}
private static void RetrieveMethod(Type t, String name, BindingFlags flags)
{
try
{
MethodInfo m = t.GetMethod(name, flags);
if (m != null)
{
Console.Write("{0}.{1}(", t.Name, m.Name);
ParameterInfo[] parms = m.GetParameters();
for (int ctr = 0; ctr < parms.Length; ctr++)
{
Console.Write(parms[ctr].ParameterType.Name);
if (ctr < parms.Length - 1)
Console.Write(", ");
}
Console.WriteLine(")");
}
else
{
Console.WriteLine("Method not found");
}
}
catch (AmbiguousMatchException)
{
Console.WriteLine("The following duplicate matches were found:");
MethodInfo[] methods = t.GetMethods(flags);
foreach (var method in methods)
{
if (method.Name != name) continue;
Console.Write(" {0}.{1}(", t.Name, method.Name);
ParameterInfo[] parms = method.GetParameters();
for (int ctr = 0; ctr < parms.Length; ctr++)
{
Console.Write(parms[ctr].ParameterType.Name);
if (ctr < parms.Length - 1)
Console.Write(", ");
}
Console.WriteLine(")");
}
}
Console.WriteLine();
}
}
// The example displays the following output:
// The following duplicate matches were found:
// TestClass.DisplayValue(String)
// TestClass.DisplayValue(String, Object[])
//
// The following duplicate matches were found:
// TestClass.Equals(TestClass)
// TestClass.Equals(Object)
//
// TestClass.Equals(TestClass)
//
// TestClass.Equals(TestClass, TestClass)
open System
open System.Reflection
type TestClass() =
member _.DisplayValue(s) =
printfn $"%s{s}"
member _.DisplayValue(s: string, [<ParamArray>]values: obj[]) =
Console.WriteLine(s, values)
member this.Equals(t: TestClass) =
Object.ReferenceEquals(this, t)
static member Equals(t1: TestClass, t2: TestClass) =
Object.ReferenceEquals(t1, t2)
let retrieveMethod (t: Type) name (flags: BindingFlags) =
try
let m = t.GetMethod(name, flags)
if m <> null then
printf $"{t.Name}.{m.Name}("
let parms = m.GetParameters()
for i = 0 to parms.Length - 1 do
printf $"{parms[i].ParameterType.Name}"
if i < parms.Length - 1 then
printf ", "
printfn ")"
else
printfn "Method not found"
with :? AmbiguousMatchException ->
printfn "The following duplicate matches were found:"
let methods = t.GetMethods flags
for method in methods do
if method.Name = name then
printf $" {t.Name}.{method.Name}("
let parms = method.GetParameters()
for i = 0 to parms.Length - 1 do
printf $"{parms[i].ParameterType.Name}"
if i < parms.Length - 1 then
printf ", "
printfn ")"
printfn ""
let t = typeof<TestClass>
retrieveMethod t "DisplayValue" (BindingFlags.Public ||| BindingFlags.Instance)
retrieveMethod t "Equals" (BindingFlags.Public ||| BindingFlags.Instance)
retrieveMethod t "Equals" (BindingFlags.Public ||| BindingFlags.Instance ||| BindingFlags.DeclaredOnly)
retrieveMethod t "Equals" (BindingFlags.Public ||| BindingFlags.Static)
// The example displays the following output:
// The following duplicate matches were found:
// TestClass.DisplayValue(String)
// TestClass.DisplayValue(String, Object[])
//
// The following duplicate matches were found:
// TestClass.Equals(TestClass)
// TestClass.Equals(Object)
//
// TestClass.Equals(TestClass)
//
// TestClass.Equals(TestClass, TestClass)
Imports System.Reflection
Public Class TestClass
Public Sub DisplayValue(s As String)
Console.WriteLine(s)
End Sub
Public Sub DisplayValue(s As String, ParamArray values() As Object)
Console.WriteLine(s, values)
End Sub
Public Overloads Shared Function Equals(t1 As TestClass, t2 As TestClass) As Boolean
Return Object.ReferenceEquals(t1, t2)
End Function
Public Overloads Function Equals(t As TestClass) As Boolean
Return Object.ReferenceEquals(Me, t)
End Function
End Class
Module Example
Public Sub Main()
Dim t As Type = GetType(TestClass)
RetrieveMethod(t, "DisplayValue", BindingFlags.Public Or BindingFlags.Instance)
RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Instance)
RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Static)
End Sub
Public Sub RetrieveMethod(t As Type, name As String, flags As BindingFlags)
Try
Dim m As MethodInfo = t.GetMethod(name, flags)
If m IsNot Nothing Then
Console.Write("{0}.{1}(", t.Name, m.Name)
Dim parms() As ParameterInfo = m.GetParameters()
For ctr As Integer = 0 To parms.Length - 1
Console.Write(parms(ctr).ParameterType.Name)
if ctr < parms.Length - 1 Then
Console.Write(", ")
End If
Next
Console.WriteLine(")")
Else
Console.WriteLine("Method not found")
End If
Catch e As AmbiguousMatchException
Console.WriteLine("The following duplicate matches were found:")
Dim methods() As MethodInfo = t.GetMethods(flags)
For Each method In methods
If method.Name <> name Then Continue For
Console.Write(" {0}.{1}(", t.Name, method.Name)
Dim parms() As ParameterInfo = method.GetParameters()
For ctr As Integer = 0 To parms.Length - 1
Console.Write(parms(ctr).ParameterType.Name)
if ctr < parms.Length - 1 Then
Console.Write(", ")
End If
Next
Console.WriteLine(")")
Next
End Try
Console.WriteLine()
End Sub
End Module
' The example displays the following output:
' The following duplicate matches were found:
' TestClass.DisplayValue(String)
' TestClass.DisplayValue(String, Object[])
'
' The following duplicate matches were found:
' TestClass.Equals(TestClass)
' TestClass.Equals(Object)
'
' TestClass.Equals(TestClass)
'
' TestClass.Equals(TestClass, TestClass)
다음 중 하나를 수행하여 특정 메서드를 검색할 수 있습니다.
바인딩 제약 조건을 변경합니다. 이전 예제에서는 형식에 의해 선언되고 상속되지 않은 공용 인스턴스
Equals메서드를 검색하려고 시도하면 성공적으로 검색됩니다Equals(TestClass).메서드 매개 변수의 형식을 GetMethod
types정의하는 매개 변수를 포함하는 메서드의 오버로드를 호출합니다.메서드를 GetMethods(BindingFlags) 호출하여 지정된 바인딩 특성이 있는 형식에 속하는 모든 메서드를 포함하는 배열을 검색합니다. 그런 다음 반복하여 이름이 중복
name된 메서드를 식별할 수 있습니다. 이 방법은 예외에 대한 이전 예제의 처리기에 설명되어 있습니다 AmbiguousMatchException .
현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 적절한 형식 인수로 대체된 형식 매개 변수를 반환 MethodInfo 합니다.
현재 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 메서드 또는 클래스 제약 조건이 없는 경우의 Object 메서드를 검색합니다.
메모
제네릭 메서드의 경우 형식 인수 name를 포함하지 마세요. 예를 들어 C# 코드 GetMember("MyMethod<int>") 는 형식int의 제네릭 인수가 하나 있는 메서드가 아니라 텍스트 이름이 "MyMethod<int>"인 MyMethod 멤버를 검색합니다.
추가 정보
- MethodInfo
- String
- BindingFlags
- DefaultBinder
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
적용 대상
GetMethod(String, BindingFlags, Type[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
지정된 바인딩 제약 조건을 사용하여 매개 변수가 지정된 인수 형식과 일치하는 지정된 메서드를 검색합니다.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * System.Reflection.BindingFlags * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, types As Type()) As MethodInfo
매개 변수
- name
- String
가져올 메서드의 이름을 포함하는 문자열입니다.
- bindingAttr
- BindingFlags
검색을 수행하는 방법을 지정하는 열거형 값의 비트 조합입니다.
-또는- 기본값을 반환 null합니다.
- types
- Type[]
가져올 메서드에 Type 대한 매개 변수의 수, 순서 및 형식을 나타내는 개체의 배열입니다. -또는- 매개 변수를 사용하지 않는 메서드를 가져오는 빈 개체 배열 Type (필드에서 제공 EmptyTypes )입니다.
반환
지정된 요구 사항과 일치하는 메서드를 나타내는 개체입니다(있는 경우). 그렇지 않으면 . null
- 특성
적용 대상
GetMethod(String, Int32, Type[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
매개 변수가 지정된 제네릭 매개 변수 수 및 인수 형식과 일치하는 지정된 public 메서드를 검색합니다.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types);
public System.Reflection.MethodInfo GetMethod(string name, int genericParameterCount, Type[] types);
public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * int * Type[] -> System.Reflection.MethodInfo
member this.GetMethod : string * int * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, types As Type()) As MethodInfo
매개 변수
- name
- String
가져올 public 메서드의 이름을 포함하는 문자열입니다.
- genericParameterCount
- Int32
메서드의 제네릭 형식 매개 변수 수입니다.
- types
- Type[]
가져올 메서드에 Type 대한 매개 변수의 수, 순서 및 형식을 나타내는 개체의 배열입니다.
-또는-
매개 변수를 사용하지 않는 메서드를 가져오는 빈 개체 배열 Type (필드에서 제공 EmptyTypes )입니다.
반환
매개 변수가 지정된 제네릭 매개 변수 개수 및 인수 형식과 일치하는 public 메서드를 나타내는 개체입니다(있는 경우). 그렇지 않으면 . null
- 특성
예외
genericParameterCount 가 음수입니다.
적용 대상
GetMethod(String, Type[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
매개 변수가 지정된 인수 형식과 일치하는 지정된 public 메서드를 검색합니다.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types);
public:
virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, Type[] types);
public System.Reflection.MethodInfo GetMethod(string name, Type[] types);
public System.Reflection.MethodInfo? GetMethod(string name, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * Type[] -> System.Reflection.MethodInfo
member this.GetMethod : string * Type[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * Type[] -> System.Reflection.MethodInfo
override this.GetMethod : string * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, types As Type()) As MethodInfo
매개 변수
- name
- String
가져올 public 메서드의 이름을 포함하는 문자열입니다.
- types
- Type[]
가져올 메서드에 Type 대한 매개 변수의 수, 순서 및 형식을 나타내는 개체의 배열입니다.
-또는-
매개 변수를 사용하지 않는 메서드를 가져오는 빈 개체 배열 Type (필드에서 제공 EmptyTypes )입니다.
반환
매개 변수가 지정된 인수 형식과 일치하는 public 메서드를 나타내는 개체입니다(있는 경우). 그렇지 않으면 . null
구현
- 특성
예외
지정된 이름과 지정된 매개 변수를 사용하여 두 개 이상의 메서드를 찾습니다.
types 가 다차원입니다.
예제
다음 예제에서는 다양한 인수 형식을 MethodA지정하는 특정 오버로드를 찾습니다.
메모
Visual C# 2005 예제에는 /unsafe 컴파일러 옵션이 필요합니다.
using System;
using System.Reflection;
class Program4
{
// Methods to get:
public void MethodA(int i, int j) { }
public void MethodA(int[] i) { }
public unsafe void MethodA(int* i) { }
public void MethodA(ref int r) {}
// Method that takes an out parameter:
public void MethodA(int i, out int o) { o = 100;}
static void Main(string[] args)
{
MethodInfo mInfo;
// Get MethodA(int i, int i)
mInfo = typeof(Program).GetMethod("MethodA",
new Type[] { typeof(int), typeof(int) });
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(int[] i)
mInfo = typeof(Program).GetMethod("MethodA",
new Type[] { typeof(int[]) });
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(int* i)
mInfo = typeof(Program).GetMethod("MethodA",
new Type[] { typeof(int).MakePointerType() });
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(ref int r)
mInfo = typeof(Program).GetMethod("MethodA",
new Type[] { typeof(int).MakeByRefType() });
Console.WriteLine("Found method: {0}", mInfo);
// Get MethodA(int i, out int o)
mInfo = typeof(Program).GetMethod("MethodA",
new Type[] { typeof(int), typeof(int).MakeByRefType() });
Console.WriteLine("Found method: {0}", mInfo);
}
}
type Program() =
// Methods to get:
member _.MethodA(i: int, j: int) = ()
member _.MethodA(i: int[]) = ()
member _.MethodA(i: int nativeptr) = ()
member _.MethodA(r: int byref) = ()
// Method that takes an outref parameter:
member _.MethodA(i: int, o: int outref) = o <- 100
do
// member MethodA: i: int * j: int -> unit
let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>; typeof<int> |])
printfn $"Found method: {mInfo}"
// member MethodA: i: int[] -> unit
let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int[]> |])
printfn $"Found method: {mInfo}"
// member MethodA: i: nativeptr<int> -> unit
let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>.MakePointerType() |])
printfn $"Found method: {mInfo}"
// member MethodA: r: byref<int> -> unit
let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>.MakeByRefType() |])
printfn $"Found method: {mInfo}"
// member MethodA: i: int * o: outref<int> -> unit
let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>; typeof<int>.MakeByRefType() |])
printfn $"Found method: {mInfo}"
Imports System.Reflection
Imports System.Runtime.InteropServices
Class Program
' Methods to get:
Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)
End Sub
Public Overloads Sub MethodA(ByVal i() As Integer)
End Sub
Public Overloads Sub MethodA(ByRef r As Integer)
End Sub
' Method that takes an out parameter. Note that an Imports
' reference is needed to System.Runtime.InteropServices
' for the <OutAttribute>, which can be shortened to <Out>.
Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
o = 100
End Sub
Public Shared Sub Main(ByVal args() As String)
Dim mInfo As MethodInfo
' Get MethodA(i As Integer i, l As Long)
mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), GetType(Long)})
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(i As Integer())
mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer())})
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(ByRef r As Integer)
mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer).MakeByRefType})
Console.WriteLine("Found method: {0}", mInfo)
' Get MethodA(i As Integer, ByRef r As Integer)
mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), _
GetType(Integer).MakeByRefType})
Console.WriteLine("Found method: {0}", mInfo)
End Sub
End Class
다음 예제에서는 제네릭이 아닌 형식(클래스), 열린 제네릭 형식(ArrayList클래스) 및 닫힌 제네릭 형식(List<T>형식)List(Of String)의 메서드를 나타내는 Add 개체를 검색 MethodInfo 합니다.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
public class Example
{
public static void Main()
{
// Get a Type object that represents a non-generic type.
GetAddMethod(typeof(ArrayList));
var list = new List<String>();
// Get a Type object that represents a constructed generic type.
Type closed = list.GetType();
GetAddMethod(closed);
// Get a Type object that represents an open generic type.
Type open = typeof(List<>);
GetAddMethod(open);
}
private static void GetAddMethod(Type typ)
{
MethodInfo method;
// Determine if this is a generic type.
if (typ.IsGenericType) {
// Is it an open generic type?
if (typ.ContainsGenericParameters)
method = typ.GetMethod("Add", typ.GetGenericArguments());
// Get closed generic type arguments.
else
method = typ.GetMethod("Add", typ.GenericTypeArguments);
}
// This is not a generic type.
else {
method = typ.GetMethod("Add", new Type[] { typeof(Object) } );
}
// Test if an Add method was found.
if (method == null) {
Console.WriteLine("No Add method found.");
return;
}
Type t = method.ReflectedType;
Console.Write("{0}.{1}.{2}(", t.Namespace, t.Name, method.Name);
ParameterInfo[] parms = method.GetParameters();
for (int ctr = 0; ctr < parms.Length; ctr++)
Console.Write("{0}{1}", parms[ctr].ParameterType.Name,
ctr < parms.Length - 1 ? ", " : "");
Console.WriteLine(")");
}
}
// The example displays the following output:
// System.Collections.ArrayList.Add(Object)
// System.Collections.Generic.List`1.Add(String)
// System.Collections.Generic.List`1.Add(T)
open System
open System.Collections
let getAddMethod (typ: Type) =
let method =
// Determine if this is a generic type.
if typ.IsGenericType then
// Is it an open generic type?
if typ.ContainsGenericParameters then
typ.GetMethod("Add", typ.GetGenericArguments())
// Get closed generic type arguments.
else
typ.GetMethod("Add", typ.GenericTypeArguments)
// This is not a generic type.
else
typ.GetMethod("Add", [| typeof<obj> |])
// Test if an Add method was found.
if method = null then
printfn "No Add method found."
else
let t = method.ReflectedType
printf $"{t.Namespace}.{t.Name}.{method.Name}("
let parms = method.GetParameters()
for i = 0 to parms.Length - 1 do
printf $"""{parms[i].ParameterType.Name}{if i < parms.Length - 1 then ", " else ""}"""
printfn ")"
// Get a Type object that represents a non-generic type.
getAddMethod typeof<ArrayList>
let list = ResizeArray<String>()
// Get a Type object that represents a constructed generic type.
let closed = list.GetType()
getAddMethod closed
// Get a Type object that represents an open generic type.
let opn = typeof<ResizeArray<_>>.GetGenericTypeDefinition()
getAddMethod opn
// The example displays the following output:
// System.Collections.ArrayList.Add(Object)
// System.Collections.Generic.List`1.Add(String)
// System.Collections.Generic.List`1.Add(T)
Imports System.Collections
Imports System.Collections.Generic
Imports System.Reflection
Module Example
Public Sub Main()
' Get a Type object that represents a non-generic type.
GetAddMethod(GetType(ArrayList))
Dim list As New List(Of String)()
' Get a Type object that represents a constructed generic type.
Dim closed As Type = list.GetType()
GetAddMethod(closed)
' Get a Type object that represents an open generic type.
Dim open As Type = GetType(List(Of))
GetAddMethod(open)
End Sub
Private Sub GetAddMethod(typ As Type)
Dim method As MethodInfo
' Determine if this is a generic type.
If typ.IsGenericType Then
' Is it an open generic type?
If typ.ContainsGenericParameters Then
method = typ.GetMethod("Add", typ.GetGenericArguments())
' Get closed generic type arguments.
Else
method = typ.GetMethod("Add", typ.GenericTypeArguments)
End If
' This is not a generic type.
Else
method = typ.GetMethod("Add", { GetType(Object) } )
End If
' Test if an Add method was found.
If method Is Nothing Then
Console.WriteLine("No Add method found.")
Exit Sub
End If
Dim t As Type = method.ReflectedType
Console.Write("{0}.{1}.{2}(", t.Namespace, t.Name, method.Name)
Dim params() As ParameterInfo = method.GetParameters()
For ctr As Integer = 0 To params.Length - 1
Console.Write("{0}{1}", params(ctr).ParameterType.Name,
If(ctr < params.Length - 1, ", ", ""))
Next
Console.WriteLine(")")
End Sub
End Module
' The example displays the following output:
' System.Collections.ArrayList.Add(Object)
' System.Collections.Generic.List`1.Add(String)
' System.Collections.Generic.List`1.Add(T)
이 예제에서는 적절한 MethodInfo 개체를 GetAddMethod 검색하는 메서드를 정의합니다. 열린 제네릭 형식에 대한 인수를 제공하기 types 위해 메서드를 호출합니다 Type.GetGenericArguments . 닫힌 types 제네릭 형식에 대한 인수를 제공하려면 속성 값을 Type.GenericTypeArguments 검색합니다.
설명
검색에서 name은 대/소문자를 구분합니다. 검색에는 공용 정적 및 공용 인스턴스 메서드가 포함됩니다.
메모
생성자와 메서드를 조회할 때는 매개 변수를 생략할 수 없습니다. 호출할 때만 매개 변수를 생략할 수 있습니다.
현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 적절한 형식 인수로 대체된 형식 매개 변수를 반환 MethodInfo 합니다.
현재 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 메서드 또는 클래스 제약 조건이 없는 경우의 Object 메서드를 검색합니다.
메모
매개 변수에는 name 형식 인수를 포함할 수 없습니다. 예를 들어 C# 코드 GetMethod("MyGenericMethod<int>") 는 형식int의 제네릭 인수가 하나 있는 메서드가 아니라 텍스트 이름이 "MyGenericMethod<int>"인 MyGenericMethod 메서드를 검색합니다. 대신 배열에서 적절한 매개 변수 types 와 함께 사용합니다GetMethod("MyGenericMethod").
추가 정보
- MethodInfo
- DefaultBinder
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
적용 대상
GetMethod(String)
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
지정된 이름의 public 메서드를 검색합니다.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
public:
virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name);
public System.Reflection.MethodInfo GetMethod(string name);
public System.Reflection.MethodInfo? GetMethod(string name);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string -> System.Reflection.MethodInfo
member this.GetMethod : string -> System.Reflection.MethodInfo
abstract member GetMethod : string -> System.Reflection.MethodInfo
override this.GetMethod : string -> System.Reflection.MethodInfo
Public Function GetMethod (name As String) As MethodInfo
매개 변수
- name
- String
가져올 public 메서드의 이름을 포함하는 문자열입니다.
반환
지정된 이름을 가진 public 메서드를 나타내는 개체입니다(있는 경우). 그렇지 않으면 . null
구현
- 특성
예외
지정된 이름을 사용하여 두 개 이상의 메서드를 찾습니다.
name은 null입니다.
예제
다음 예제에서는 라는 MethodA메서드를 가져옵니다.
using System;
using System.Reflection;
class Program
{
// Method to get:
public void MethodA() { }
static void Main(string[] args)
{
// Get MethodA()
MethodInfo mInfo = typeof(Program).GetMethod("MethodA");
Console.WriteLine("Found method: {0}", mInfo);
}
}
type Program() =
// Method to get:
member _.MethodA() = ()
// Get MethodA()
let mInfo = typeof<Program>.GetMethod "MethodA"
printfn $"Found method: {mInfo}"
Imports System.Reflection
Class Program
' Method to get:
Public Sub MethodA()
End Sub
Public Shared Sub Main(ByVal args() As String)
' Get MethodA()
Dim mInfo As MethodInfo = GetType(Program).GetMethod("MethodA")
Console.WriteLine("Found method: {0}", mInfo)
End Sub
End Class
설명
검색에서 name은 대/소문자를 구분합니다. 검색에는 공용 정적 및 공용 인스턴스 메서드가 포함됩니다.
메서드가 오버로드되고 둘 이상의 public 메서드가 있는 경우 메서드는 GetMethod(String) 예외를 AmbiguousMatchException throw합니다. 다음 예제에서는 메서드의 공용 오버로드가 둘 이상 있으므로 예외가 Int32.ToString throw됩니다. 반면에 메서드가 재정 Object.ToString 의 Person.ToString 되므로 오버로드되지 GetMethod(String) 않으므로 메서드는 개체를 MethodInfo 검색할 수 있습니다.
using System;
using System.Reflection;
public class Person
{
public String FirstName;
public String LastName;
public override String ToString()
{
return (FirstName + " " + LastName).Trim();
}
}
public class Example2
{
public static void Main()
{
Type t = typeof(Person);
RetrieveMethod(t, "ToString");
t = typeof(Int32);
RetrieveMethod(t, "ToString");
}
private static void RetrieveMethod(Type t, String name)
{
try
{
MethodInfo m = t.GetMethod(name);
if (m != null)
Console.WriteLine("{0}.{1}: {2} method", m.ReflectedType.Name,
m.Name, m.IsStatic ? "Static" : "Instance");
else
Console.WriteLine("{0}.ToString method not found", t.Name);
}
catch (AmbiguousMatchException)
{
Console.WriteLine("{0}.{1} has multiple public overloads.",
t.Name, name);
}
}
}
// The example displays the following output:
// Person.ToString: Instance method
// Int32.ToString has multiple public overloads.
open System
open System.Reflection
type Person() =
member val FirstName = "" with get, set
member val LastName = "" with get, set
override this.ToString() =
(this.FirstName + " " + this.LastName).Trim()
let retrieveMethod (t: Type) name =
try
let m = t.GetMethod name
if m <> null then
printfn $"""{m.ReflectedType.Name}.{m.Name}: {if m.IsStatic then "Static" else "Instance"} method"""
else
printfn $"{t.Name}.ToString method not found"
with :? AmbiguousMatchException ->
printfn $"{t.Name}.{name} has multiple public overloads."
let t = typeof<Person>
retrieveMethod t "ToString"
let t2 = typeof<int>
retrieveMethod t2 "ToString"
// The example displays the following output:
// Person.ToString: Instance method
// Int32.ToString has multiple public overloads.
Imports System.Reflection
Public Class Person
Public FirstName As String
Public LastName As String
Public Overrides Function ToString() As String
Return (FirstName + " " + LastName).Trim()
End Function
End Class
Module Example
Public Sub Main()
Dim t As Type = GetType(Person)
RetrieveMethod(t, "ToString")
t = GetType(Int32)
RetrieveMethod(t, "ToString")
End Sub
Private Sub RetrieveMethod(t As Type, name As String)
Try
Dim m As MethodInfo = t.GetMethod(name)
If m IsNot Nothing Then
Console.WriteLine("{0}.{1}: {2} method", m.ReflectedType.Name,
m.Name, If(m.IsStatic, "Static", "Instance"))
Else
Console.WriteLine("{0}.ToString method not found", t.Name)
End If
Catch e As AmbiguousMatchException
Console.WriteLine("{0}.{1} has multiple public overloads.",
t.Name, name)
End Try
End Sub
End Module
' The example displays the following output:
' Person.ToString: Instance method
' Int32.ToString has multiple public overloads.
다음 중 하나를 수행하여 특정 메서드를 검색할 수 있습니다.
메서드를 GetMethod(String, BindingFlags) 호출하고 메서드를
bindingAttr고유하게 식별하는 인수를 지정합니다. 예를 들어 형식에 정적 및 인스턴스 오버로드가 있기 때문에 예외가 throw되는 경우 인수 BindingFlags.Static 를 BindingFlags.Instance지정할bindingAttr수 있습니다.메서드 매개 변수의 형식을 GetMethod
types정의하는 매개 변수를 포함하는 메서드의 오버로드를 호출합니다.메서드를 GetMethods() 호출하여 형식에 속하는 모든 public 메서드를 포함하는 배열을 검색합니다. 그런 다음 반복하여 이름이 중복
name된 메서드를 식별할 수 있습니다.
현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 적절한 형식 인수로 대체된 형식 매개 변수를 반환 MethodInfo 합니다.
현재 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 메서드 또는 클래스 제약 조건이 없는 경우의 Object 메서드를 검색합니다.
메모
제네릭 메서드의 경우 형식 인수 name를 포함하지 마세요. 예를 들어 C# 코드 GetMember("MyMethod<int>") 는 형식int의 제네릭 인수가 하나 있는 메서드가 아니라 텍스트 이름이 "MyMethod<int>"인 MyMethod 멤버를 검색합니다.
추가 정보
- MethodInfo
- String
- DefaultBinder
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
적용 대상
GetMethod(String, Type[], ParameterModifier[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
매개 변수가 지정된 인수 형식 및 한정자와 일치하는 지정된 public 메서드를 검색합니다.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public System.Reflection.MethodInfo? GetMethod(string name, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod(string name, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.MethodInfo? GetMethod(string name, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)>]
member this.GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
member this.GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, types As Type(), modifiers As ParameterModifier()) As MethodInfo
매개 변수
- name
- String
가져올 public 메서드의 이름을 포함하는 문자열입니다.
- types
- Type[]
가져올 메서드에 Type 대한 매개 변수의 수, 순서 및 형식을 나타내는 개체의 배열입니다.
-또는-
매개 변수를 사용하지 않는 메서드를 가져오는 빈 개체 배열 Type (필드에서 제공 EmptyTypes )입니다.
- modifiers
- ParameterModifier[]
배열의 ParameterModifier 해당 요소와 연결된 특성을 나타내는 개체의 types 배열입니다. COM interop을 통해 호출할 때만 사용되며 참조로 전달되는 매개 변수만 처리됩니다. 기본 바인더는 이 매개 변수를 처리하지 않습니다.
반환
지정된 요구 사항과 일치하는 public 메서드를 나타내는 개체입니다(있는 경우). 그렇지 않으면 . null
구현
- 특성
예외
지정된 이름과 지정된 매개 변수를 사용하여 두 개 이상의 메서드를 찾습니다.
설명
기본 바인더는 ParameterModifier를 처리하지 않지만 (modifiers 매개 변수) 추상 System.Reflection.Binder 클래스를 사용하여 modifiers를 처리하는 사용자 지정 바인더를 작성할 수 있습니다.
ParameterModifier 는 COM interop을 통해 호출할 때만 사용되며 참조로 전달되는 매개 변수만 처리됩니다.
검색에서 name은 대/소문자를 구분합니다. 검색에는 공용 정적 및 공용 인스턴스 메서드가 포함됩니다.
메모
생성자와 메서드를 조회할 때는 매개 변수를 생략할 수 없습니다. 호출할 때만 매개 변수를 생략할 수 있습니다.
현재 Type 가 생성된 제네릭 형식을 나타내는 경우 이 메서드는 적절한 형식 인수로 대체된 형식 매개 변수를 반환 MethodInfo 합니다.
현재 Type 제네릭 형식 또는 제네릭 메서드 정의에서 형식 매개 변수를 나타내는 경우 이 메서드는 클래스 제약 조건의 메서드 또는 클래스 제약 조건이 없는 경우의 Object 메서드를 검색합니다.
메모
제네릭 메서드의 경우 형식 인수 name를 포함하지 마세요. 예를 들어 C# 코드 GetMethod("MyMethod<int>") 는 형식int의 제네릭 인수가 하나 있는 메서드가 아니라 텍스트 이름이 "MyMethod<int>"인 MyMethod 멤버를 검색합니다. 대신 배열에서 적절한 매개 변수 types 와 함께 사용합니다GetMethod("MyMethod").
추가 정보
- MethodInfo
- String
- DefaultBinder
- ParameterModifier
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()