Type.GetMethod 메서드
정의
오버로드
GetMethod(String, Int32, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) |
지정된 바인딩 제약 조건과 지정된 호출 규칙을 활용하여, 지정된 제네릭 매개 변수의 수, 인수 형식 및 한정자와 매개 변수가 일치하는 지정된 메서드를 검색합니다.Searches for the specified method whose parameters match the specified generic parameter count, argument types and modifiers, using the specified binding constraints and the specified calling convention. |
GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) |
지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 지정된 메서드를 지정된 바인딩 제약 조건과 지정된 호출 규칙으로 검색합니다.Searches for the specified method whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention. |
GetMethod(String, Int32, BindingFlags, Binder, Type[], ParameterModifier[]) |
지정된 바인딩 제약 조건을 사용하여, 지정된 제네릭 매개 변수의 수, 인수 형식 및 한정자와 매개 변수가 일치하는 지정된 메서드를 검색합니다.Searches for the specified method whose parameters match the specified generic parameter count, argument types and modifiers, using the specified binding constraints. |
GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[]) |
지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 지정된 메서드를 지정된 바인딩 제약 조건으로 검색합니다.Searches for the specified method whose parameters match the specified argument types and modifiers, using the specified binding constraints. |
GetMethod(String, Int32, Type[], ParameterModifier[]) |
지정된 매개 변수의 수, 인수 형식 및 한정자와 매개 변수가 일치하는 지정된 공용 메서드를 검색합니다.Searches for the specified public method whose parameters match the specified generic parameter count, argument types and modifiers. |
GetMethod(String, Type[]) |
지정된 인수 형식과 일치하는 매개 변수를 가진 지정된 public 메서드를 검색합니다.Searches for the specified public method whose parameters match the specified argument types. |
GetMethod(String, Int32, Type[]) |
지정된 제네릭 매개 변수의 수 및 인수 형식과 매개 변수가 일치하는 지정된 공용 메서드를 검색합니다.Searches for the specified public method whose parameters match the specified generic parameter count and argument types. |
GetMethod(String, BindingFlags) |
지정된 메서드를 지정된 바인딩 제약 조건으로 검색합니다.Searches for the specified method, using the specified binding constraints. |
GetMethod(String) |
지정된 이름의 public 메서드를 검색합니다.Searches for the public method with the specified name. |
GetMethod(String, Type[], ParameterModifier[]) |
지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 지정된 public 메서드를 검색합니다.Searches for the specified public method whose parameters match the specified argument types and modifiers. |
GetMethod(String, Int32, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
지정된 바인딩 제약 조건과 지정된 호출 규칙을 활용하여, 지정된 제네릭 매개 변수의 수, 인수 형식 및 한정자와 매개 변수가 일치하는 지정된 메서드를 검색합니다.Searches for the specified method whose parameters match the specified generic parameter count, argument types and modifiers, using the specified binding constraints and the specified calling convention.
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);
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);
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 메서드의 이름이 포함된 문자열입니다.The string containing the name of the public method to get.
- genericParameterCount
- Int32
메서드의 제네릭 형식 매개 변수의 수입니다.The number of generic type parameters of the method.
- bindingAttr
- BindingFlags
검색 방법을 지정하는 열거형 값의 비트 조합입니다.A bitwise combination of the enumeration values that specify how the search is conducted.
또는-or-
- binder
- Binder
속성 집합을 정의하고 바인딩을 활성화하는 개체입니다. 이 개체는 또한 리플렉션을 통해 오버로드된 메서드를 선택할 수 있고 인수 형식을 강제 변환할 수 있으며 멤버를 호출할 수도 있습니다.An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.
또는-or-
Nothing
를 사용하려면 null 참조(Visual Basic의 경우 DefaultBinder)입니다.A null reference (Nothing
in Visual Basic), to use the DefaultBinder.
- callConvention
- CallingConventions
인수의 순서 및 레이아웃, 반환 값의 전달 방법, 인수에 사용되는 레지스터, 스택 정리 방법 등과 관련하여 사용할 규칙을 지정하는 개체입니다.The object that specifies the set of rules to use regarding the order and layout of arguments, how the return value is passed, what registers are used for arguments, and how the stack is cleaned up.
- types
- Type[]
가져올 메서드에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.An array of Type objects representing the number, order, and type of the parameters for the method to get.
또는-or-
매개 변수를 사용하지 않는 메서드를 가져올 Type 개체(EmptyTypes 필드에서 제공됨)의 빈 배열입니다.An empty array of Type objects (as provided by the EmptyTypes field) to get a method that takes no parameters.
- modifiers
- ParameterModifier[]
ParameterModifier 배열에서 해당하는 요소와 연관된 특성을 나타내는 types
개체 배열입니다.An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types
array. COM interop를 통해 호출하는 경우에만 사용되며 참조에 의해 전달된 매개 변수만 처리합니다.To be only used when calling through COM interop, and only parameters that are passed by reference are handled. 기본 바인더에서는 이 매개 변수를 처리하지 않습니다.The default binder does not process this parameter.
반환
(발견된 경우) 지정된 제네릭 매개 변수의 수, 인수 형식, 한정자, 바인딩 제약 조건 및 호출 규칙과 일치하는 메서드를 나타내는 개체입니다. 그 밖의 경우에는 null
.An object representing the method that matches the specified generic parameter count, argument types, modifiers, binding constraints and calling convention, if found; otherwise, null
.
예외
name
이(가) null
인 경우name
is null
.
또는-or-
types
이(가) null
인 경우types
is null
.
또는-or-
types
배열의 요소 중 하나가 null
입니다.One of the elements in the types
array is null
.
genericParameterCount
가 음수입니다.genericParameterCount
is negative.
적용 대상
GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 지정된 메서드를 지정된 바인딩 제약 조건과 지정된 호출 규칙으로 검색합니다.Searches for the specified method whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention.
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);
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);
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
가져올 메서드의 이름이 포함된 문자열입니다.The string containing the name of the method to get.
- bindingAttr
- BindingFlags
검색 방법을 지정하는 열거형 값의 비트 조합입니다.A bitwise combination of the enumeration values that specify how the search is conducted.
- binder
- Binder
속성 집합을 정의하고 바인딩을 활성화하는 개체입니다. 이 개체는 또한 리플렉션을 통해 오버로드된 메서드를 선택할 수 있고 인수 형식을 강제 변환할 수 있으며 멤버를 호출할 수도 있습니다.An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.
또는-or-
Nothing
를 사용하려면 null 참조(Visual Basic의 경우 DefaultBinder)입니다.A null reference (Nothing
in Visual Basic), to use the DefaultBinder.
- callConvention
- CallingConventions
인수의 순서 및 레이아웃, 반환 값의 전달 방법, 인수에 사용되는 레지스터, 스택 정리 방법 등과 관련하여 사용할 규칙을 지정하는 개체입니다.The object that specifies the set of rules to use regarding the order and layout of arguments, how the return value is passed, what registers are used for arguments, and how the stack is cleaned up.
- types
- Type[]
가져올 메서드에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.An array of Type objects representing the number, order, and type of the parameters for the method to get.
또는-or- 매개 변수를 사용하지 않는 메서드를 가져올 Type 개체(EmptyTypes 필드에서 제공됨)의 빈 배열입니다.An empty array of Type objects (as provided by the EmptyTypes field) to get a method that takes no parameters.
- modifiers
- ParameterModifier[]
ParameterModifier 배열에서 해당하는 요소와 연관된 특성을 나타내는 types
개체 배열입니다.An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types
array. COM interop를 통해 호출하는 경우에만 사용되며 참조에 의해 전달된 매개 변수만 처리합니다.To be only used when calling through COM interop, and only parameters that are passed by reference are handled. 기본 바인더에서는 이 매개 변수를 처리하지 않습니다.The default binder does not process this parameter.
반환
지정된 요구 사항과 일치하는 메서드를 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null
을 반환합니다.An object representing the method that matches the specified requirements, if found; otherwise, null
.
구현
예외
지정한 이름을 가지며 지정된 바인딩 제약 조건과 일치하는 메서드가 둘 이상 발견되었습니다.More than one method is found with the specified name and matching the specified binding constraints.
name
이(가) null
인 경우name
is null
.
또는-or-
types
이(가) null
인 경우types
is null
.
또는-or-
types
의 요소 중 하나가 null
입니다.One of the elements in types
is null
.
types
가 다차원 배열인 경우types
is multidimensional.
또는-or-
modifiers
가 다차원 배열인 경우modifiers
is multidimensional.
예제
다음 예제에서는 MethodA
바인딩 제약 조건, 호출 규칙 및 다양 한 인수 형식을 지정 하 여의 특정 오버 로드를 찾습니다.The following example finds specific overloads of MethodA
, specifying binding constraints, calling conventions, and a variety of argument types.
참고
이 Visual C# 2005Visual C# 2005 예제에는 /unsafe
컴파일러 옵션이 필요 합니다.The Visual C# 2005Visual C# 2005 example requires the /unsafe
compiler option.
using namespace System;
using namespace System::Reflection;
public ref class Program
{
public:
// Methods to get:
void MethodA(int i, int j) { }
void MethodA(array<int>^ iarry) { }
void MethodA(double *ip) { }
// Method that takes a managed reference paramter.
void MethodA(int% r) {}
};
int main()
{
MethodInfo^ mInfo;
// Get MethodA(int i, int j)
mInfo = Program::typeid->GetMethod("MethodA",
BindingFlags::Public | BindingFlags::Instance,
nullptr,
CallingConventions::Any,
gcnew array<Type^> {int::typeid, int::typeid},
nullptr);
Console::WriteLine("Found method: {0}", mInfo );
// Get MethodA(array<int>^ iarry)
mInfo = Program::typeid->GetMethod("MethodA",
BindingFlags::Public | BindingFlags::Instance,
nullptr,
CallingConventions::Any,
gcnew array<Type^> {int::typeid->MakeArrayType()},
nullptr);
Console::WriteLine("Found method: {0}", mInfo );
// Get MethodA(double *ip)
mInfo = Program::typeid->GetMethod("MethodA",
BindingFlags::Public | BindingFlags::Instance,
nullptr,
CallingConventions::Any,
gcnew array<Type^> {double::typeid->MakePointerType()},
nullptr);
Console::WriteLine("Found method: {0}", mInfo );
// Get MethodA(int% r)
mInfo = Program::typeid->GetMethod("MethodA",
BindingFlags::Public | BindingFlags::Instance,
nullptr,
CallingConventions::Any,
gcnew array<Type^> {int::typeid->MakeByRefType()},
nullptr);
Console::WriteLine("Found method: {0}", mInfo );
}
using System;
using System.Reflection;
class Program
{
// 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);
}
}
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
.Although the default binder does not process ParameterModifier (the modifiers
parameter), you can use the abstract System.Reflection.Binder class to write a custom binder that does process modifiers
. ParameterModifier
는 COM interop를 통해 호출 하는 경우에만 사용 되며 참조로 전달 되는 매개 변수만 처리 합니다.ParameterModifier
is only used when calling through COM interop, and only parameters that are passed by reference are handled.
다음 표에서는 GetXXX
형식에 대해 리플렉션할 때 메서드에서 반환 되는 기본 클래스의 멤버를 보여 줍니다.The following table shows what members of a base class are returned by the GetXXX
methods when reflecting on a type.
멤버 형식Member Type | 정적Static | 비정적Non-Static |
---|---|---|
생성자Constructor | 아니요No | 아니요No |
필드Field | 아니요No | 예.Yes. 필드는 항상 이름 및 시그니처로 숨겨집니다.A field is always hide-by-name-and-signature. |
이벤트Event | 해당 없음Not applicable | 공용 형식 시스템 규칙은 상속이 속성을 구현 하는 메서드와 동일 하다는 것입니다.The common type system rule is that the inheritance is the same as that of the methods that implement the property. 리플렉션에서는 속성을 이름으로 숨기기-서명으로 처리 합니다.Reflection treats properties as hide-by-name-and-signature. 아래의 참고 2를 참조 하세요.See note 2 below. |
메서드Method | 아니요No | 예.Yes. 메서드 (가상 및 비가상)는 이름을 기준으로 숨기 거 나 이름에 따라 숨길 수 있습니다.A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature. |
중첩 형식Nested Type | 아니요No | 아니요No |
속성Property | 해당 없음Not applicable | 공용 형식 시스템 규칙은 상속이 속성을 구현 하는 메서드와 동일 하다는 것입니다.The common type system rule is that the inheritance is the same as that of the methods that implement the property. 리플렉션에서는 속성을 이름으로 숨기기-서명으로 처리 합니다.Reflection treats properties as hide-by-name-and-signature. 아래의 참고 2를 참조 하세요.See note 2 below. |
이름 및 시그니처는 사용자 지정 한정자, 반환 형식, 매개 변수 형식, 센티널 및 관리 되지 않는 호출 규칙을 포함 하 여 시그니처의 모든 부분을 고려 합니다.Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 이는 이진 비교입니다.This is a binary comparison.
리플렉션의 경우 속성과 이벤트는 이름 및 시그니처로 숨겨집니다.For reflection, properties and events are hide-by-name-and-signature. 기본 클래스에 get 및 set 접근자가 모두 포함 된 속성이 있지만 파생 클래스에 get 접근자만 있는 경우 파생 클래스 속성은 기본 클래스 속성을 숨기고 기본 클래스의 setter에 액세스할 수 없게 됩니다.If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
사용자 지정 특성은 공용 형식 시스템의 일부가 아닙니다.Custom attributes are not part of the common type system.
다음 BindingFlags 필터 플래그를 사용 하 여 검색에 포함할 메서드를 정의할 수 있습니다.The following BindingFlags filter flags can be used to define which methods to include in the search:
BindingFlags.Instance
반환을 얻으려면 또는 중 하나를 지정 해야 합니다BindingFlags.Static
.You must specify eitherBindingFlags.Instance
orBindingFlags.Static
in order to get a return.BindingFlags.Public
검색에 공용 메서드를 포함 하도록 지정 합니다.SpecifyBindingFlags.Public
to include public methods in the search.BindingFlags.NonPublic
검색에 public이 아닌 메서드 (즉, private, internal 및 protected 메서드)를 포함 하도록 지정 합니다.SpecifyBindingFlags.NonPublic
to include nonpublic methods (that is, private, internal, and protected methods) in the search.BindingFlags.FlattenHierarchy
public
계층의 정적 멤버를 포함 하도록 지정 합니다. 상속 된protected
private
클래스의 정적 멤버는 포함 되지 않습니다.SpecifyBindingFlags.FlattenHierarchy
to includepublic
andprotected
static members up the hierarchy;private
static members in inherited classes are not included.
다음 BindingFlags 한정자 플래그를 사용 하 여 검색의 작동 방식을 변경할 수 있습니다.The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.IgnoreCase
의 대/소문자를 무시 하려면입니다name
.BindingFlags.IgnoreCase
to ignore the case ofname
.BindingFlags.DeclaredOnly
단순히 상속 된 메서드가 아니라에 선언 된 메서드만 검색 합니다 Type .BindingFlags.DeclaredOnly
to search only the methods declared on the Type, not methods that were simply inherited.
자세한 내용은 System.Reflection.BindingFlags를 참조하세요.See System.Reflection.BindingFlags for more information.
참고
생성자와 메서드를 조회할 때 매개 변수를 생략할 수 없습니다.You cannot omit parameters when looking up constructors and methods. 를 호출할 때에만 매개 변수를 생략할 수 있습니다.You can only omit parameters when invoking.
현재 Type 이 생성 된 제네릭 형식을 나타내는 경우이 메서드는 MethodInfo 형식 매개 변수를 적절 한 형식 인수로 대체 한를 반환 합니다.If the current Type represents a constructed generic type, this method returns the MethodInfo with the type parameters replaced by the appropriate type arguments.
현재 Type 이 제네릭 형식 또는 제네릭 메서드 정의의 형식 매개 변수를 나타내는 경우이 메서드는 클래스 제약 조건의 메서드나 Object 클래스 제약 조건이 없는 경우의 메서드를 검색 합니다.If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint.
참고
제네릭 메서드의 경우에 형식 인수를 포함 하지 마십시오 name
.For generic methods, do not include the type arguments in name
. 예를 들어 c # 코드는 GetMember("MyMethod<int>")
MyMethod<int>
MyMethod
형식의 제네릭 인수 하나를 포함 하는 라는 메서드가 아니라 텍스트 이름이 "" 인 멤버를 검색 int
합니다.For example, the C# code GetMember("MyMethod<int>")
searches for a member with the text name "MyMethod<int>
", rather than for a method named MyMethod
that has one generic argument of type int
.
추가 정보
- MethodInfo
- String
- BindingFlags
- Binder
- DefaultBinder
- CallingConventions
- ParameterModifier
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
적용 대상
GetMethod(String, Int32, BindingFlags, Binder, Type[], ParameterModifier[])
지정된 바인딩 제약 조건을 사용하여, 지정된 제네릭 매개 변수의 수, 인수 형식 및 한정자와 매개 변수가 일치하는 지정된 메서드를 검색합니다.Searches for the specified method whose parameters match the specified generic parameter count, argument types and modifiers, using the specified binding constraints.
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);
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);
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 메서드의 이름이 포함된 문자열입니다.The string containing the name of the public method to get.
- genericParameterCount
- Int32
메서드의 제네릭 형식 매개 변수의 수입니다.The number of generic type parameters of the method.
- bindingAttr
- BindingFlags
검색 방법을 지정하는 열거형 값의 비트 조합입니다.A bitwise combination of the enumeration values that specify how the search is conducted.
또는-or-
- binder
- Binder
속성 집합을 정의하고 바인딩을 활성화하는 개체입니다. 이 개체는 또한 리플렉션을 통해 오버로드된 메서드를 선택할 수 있고 인수 형식을 강제 변환할 수 있으며 멤버를 호출할 수도 있습니다.An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.
또는-or-
Nothing
를 사용하려면 null 참조(Visual Basic의 경우 DefaultBinder)입니다.A null reference (Nothing
in Visual Basic), to use the DefaultBinder.
- types
- Type[]
가져올 메서드에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.An array of Type objects representing the number, order, and type of the parameters for the method to get.
또는-or-
매개 변수를 사용하지 않는 메서드를 가져올 Type 개체(EmptyTypes 필드에서 제공됨)의 빈 배열입니다.An empty array of Type objects (as provided by the EmptyTypes field) to get a method that takes no parameters.
- modifiers
- ParameterModifier[]
ParameterModifier 배열에서 해당하는 요소와 연관된 특성을 나타내는 types
개체 배열입니다.An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types
array. COM interop를 통해 호출하는 경우에만 사용되며 참조에 의해 전달된 매개 변수만 처리합니다.To be only used when calling through COM interop, and only parameters that are passed by reference are handled. 기본 바인더에서는 이 매개 변수를 처리하지 않습니다.The default binder does not process this parameter.
반환
(발견된 경우) 지정된 제네릭 매개 변수의 수, 인수 형식, 한정자 및 바인딩 제약 조건과 일치하는 메서드를 나타내는 개체입니다. 그 밖의 경우에는 null
.An object representing the method that matches the specified generic parameter count, argument types, modifiers and binding constraints, if found; otherwise, null
.
예외
name
이(가) null
인 경우name
is null
.
또는-or-
types
이(가) null
인 경우types
is null
.
또는-or-
types
배열의 요소 중 하나가 null
입니다.One of the elements in the types
array is null
.
genericParameterCount
가 음수입니다.genericParameterCount
is negative.
적용 대상
GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])
지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 지정된 메서드를 지정된 바인딩 제약 조건으로 검색합니다.Searches for the specified method whose parameters match the specified argument types and modifiers, using the specified binding constraints.
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);
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);
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
가져올 메서드의 이름이 포함된 문자열입니다.The string containing the name of the method to get.
- bindingAttr
- BindingFlags
검색 방법을 지정하는 열거형 값의 비트 조합입니다.A bitwise combination of the enumeration values that specify how the search is conducted.
- binder
- Binder
속성 집합을 정의하고 바인딩을 활성화하는 개체입니다. 이 개체는 또한 리플렉션을 통해 오버로드된 메서드를 선택할 수 있고 인수 형식을 강제 변환할 수 있으며 멤버를 호출할 수도 있습니다.An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.
또는-or-
Nothing
를 사용하려면 null 참조(Visual Basic의 경우 DefaultBinder)입니다.A null reference (Nothing
in Visual Basic), to use the DefaultBinder.
- types
- Type[]
가져올 메서드에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.An array of Type objects representing the number, order, and type of the parameters for the method to get.
또는-or- 매개 변수를 사용하지 않는 메서드를 가져올 Type 개체(EmptyTypes 필드에서 제공됨)의 빈 배열입니다.An empty array of Type objects (as provided by the EmptyTypes field) to get a method that takes no parameters.
- modifiers
- ParameterModifier[]
ParameterModifier 배열에서 해당하는 요소와 연관된 특성을 나타내는 types
개체 배열입니다.An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types
array. COM interop를 통해 호출하는 경우에만 사용되며 참조에 의해 전달된 매개 변수만 처리합니다.To be only used when calling through COM interop, and only parameters that are passed by reference are handled. 기본 바인더에서는 이 매개 변수를 처리하지 않습니다.The default binder does not process this parameter.
반환
지정된 요구 사항과 일치하는 메서드를 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null
을 반환합니다.An object representing the method that matches the specified requirements, if found; otherwise, null
.
구현
예외
지정한 이름을 가지며 지정된 바인딩 제약 조건과 일치하는 메서드가 둘 이상 발견되었습니다.More than one method is found with the specified name and matching the specified binding constraints.
name
이(가) null
인 경우name
is null
.
또는-or-
types
이(가) null
인 경우types
is null
.
또는-or-
types
의 요소 중 하나가 null
입니다.One of the elements in types
is null
.
types
가 다차원 배열인 경우types
is multidimensional.
또는-or-
modifiers
가 다차원 배열인 경우modifiers
is multidimensional.
예제
다음 예제에서는 MethodA
바인딩 제약 조건 및 다양 한 인수 형식을 지정 하 여의 특정 오버 로드를 찾습니다.The following example finds specific overloads of MethodA
, specifying binding constraints and a variety of argument types.
참고
이 Visual C# 2005Visual C# 2005 예제에는 /unsafe
컴파일러 옵션이 필요 합니다.The Visual C# 2005Visual C# 2005 example requires the /unsafe
compiler option.
using namespace System;
using namespace System::Reflection;
public ref class Program
{
public:
// Methods to get:
void MethodA(int i, int j) { }
void MethodA(array<int>^ iarry) { }
void MethodA(double *ip) { }
// Method that takes a managed reference parameter.
void MethodA(int% r) {}
};
int main()
{
MethodInfo^ mInfo;
// Get MethodA(int i, int j)
mInfo = Program::typeid->GetMethod("MethodA",
static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
nullptr,
gcnew array<Type^> {int::typeid, int::typeid},
nullptr);
Console::WriteLine("Found method: {0}", mInfo );
// Get MethodA(array<int>^ iarry)
mInfo = Program::typeid->GetMethod("MethodA",
static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
nullptr,
gcnew array<Type^> {int::typeid->MakeArrayType()},
nullptr);
Console::WriteLine("Found method: {0}", mInfo );
// Get MethodA(double *ip)
mInfo = Program::typeid->GetMethod("MethodA",
static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
nullptr,
gcnew array<Type^> {double::typeid->MakePointerType()},
nullptr);
Console::WriteLine("Found method: {0}", mInfo );
// Get MethodA(int% r)
mInfo = Program::typeid->GetMethod("MethodA",
static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
nullptr,
gcnew array<Type^> {int::typeid->MakeByRefType()},
nullptr);
Console::WriteLine("Found method: {0}", mInfo );
}
using System;
using System.Reflection;
class Program
{
// 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);
}
}
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
.Although the default binder does not process ParameterModifier (the modifiers
parameter), you can use the abstract System.Reflection.Binder class to write a custom binder that does process modifiers
. ParameterModifier
는 COM interop를 통해 호출 하는 경우에만 사용 되며 참조로 전달 되는 매개 변수만 처리 합니다.ParameterModifier
is only used when calling through COM interop, and only parameters that are passed by reference are handled.
다음 BindingFlags 필터 플래그를 사용 하 여 검색에 포함할 메서드를 정의할 수 있습니다.The following BindingFlags filter flags can be used to define which methods to include in the search:
BindingFlags.Instance
반환을 얻으려면 또는 중 하나를 지정 해야 합니다BindingFlags.Static
.You must specify eitherBindingFlags.Instance
orBindingFlags.Static
in order to get a return.BindingFlags.Public
검색에 공용 메서드를 포함 하도록 지정 합니다.SpecifyBindingFlags.Public
to include public methods in the search.BindingFlags.NonPublic
검색에 public이 아닌 메서드 (즉, private, internal 및 protected 메서드)를 포함 하도록 지정 합니다.SpecifyBindingFlags.NonPublic
to include nonpublic methods (that is, private, internal, and protected methods) in the search.BindingFlags.FlattenHierarchy
public
계층의 정적 멤버를 포함 하도록 지정 합니다. 상속 된protected
private
클래스의 정적 멤버는 포함 되지 않습니다.SpecifyBindingFlags.FlattenHierarchy
to includepublic
andprotected
static members up the hierarchy;private
static members in inherited classes are not included.
다음 BindingFlags 한정자 플래그를 사용 하 여 검색의 작동 방식을 변경할 수 있습니다.The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.IgnoreCase
의 대/소문자를 무시 하려면입니다name
.BindingFlags.IgnoreCase
to ignore the case ofname
.BindingFlags.DeclaredOnly
단순히 상속 된 메서드가 아니라에 선언 된 메서드만 검색 합니다 Type .BindingFlags.DeclaredOnly
to search only the methods declared on the Type, not methods that were simply inherited.
자세한 내용은 System.Reflection.BindingFlags를 참조하세요.See System.Reflection.BindingFlags for more information.
참고
생성자와 메서드를 조회할 때 매개 변수를 생략할 수 없습니다.You cannot omit parameters when looking up constructors and methods. 를 호출할 때에만 매개 변수를 생략할 수 있습니다.You can only omit parameters when invoking.
현재 Type 이 생성 된 제네릭 형식을 나타내는 경우이 메서드는 MethodInfo 형식 매개 변수를 적절 한 형식 인수로 대체 한를 반환 합니다.If the current Type represents a constructed generic type, this method returns the MethodInfo with the type parameters replaced by the appropriate type arguments.
현재 Type 이 제네릭 형식 또는 제네릭 메서드 정의의 형식 매개 변수를 나타내는 경우이 메서드는 클래스 제약 조건의 메서드나 Object 클래스 제약 조건이 없는 경우의 메서드를 검색 합니다.If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint.
참고
제네릭 메서드의 경우에 형식 인수를 포함 하지 마십시오 name
.For generic methods, do not include the type arguments in name
. 예를 들어 c # 코드는 GetMember("MyMethod<int>")
MyMethod<int>
MyMethod
형식의 제네릭 인수 하나를 포함 하는 라는 메서드가 아니라 텍스트 이름이 "" 인 멤버를 검색 int
합니다.For example, the C# code GetMember("MyMethod<int>")
searches for a member with the text name "MyMethod<int>
", rather than for a method named MyMethod
that has one generic argument of type int
.
추가 정보
- MethodInfo
- String
- BindingFlags
- Binder
- DefaultBinder
- ParameterModifier
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
적용 대상
GetMethod(String, Int32, Type[], ParameterModifier[])
지정된 매개 변수의 수, 인수 형식 및 한정자와 매개 변수가 일치하는 지정된 공용 메서드를 검색합니다.Searches for the specified public method whose parameters match the specified generic parameter count, argument types and modifiers.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types, cli::array <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);
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 메서드의 이름이 포함된 문자열입니다.The string containing the name of the public method to get.
- genericParameterCount
- Int32
메서드의 제네릭 형식 매개 변수의 수입니다.The number of generic type parameters of the method.
- types
- Type[]
가져올 메서드에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.An array of Type objects representing the number, order, and type of the parameters for the method to get.
또는-or-
매개 변수를 사용하지 않는 메서드를 가져올 Type 개체(EmptyTypes 필드에서 제공됨)의 빈 배열입니다.An empty array of Type objects (as provided by the EmptyTypes field) to get a method that takes no parameters.
- modifiers
- ParameterModifier[]
ParameterModifier 배열에서 해당하는 요소와 연관된 특성을 나타내는 types
개체 배열입니다.An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types
array. COM interop를 통해 호출하는 경우에만 사용되며 참조에 의해 전달된 매개 변수만 처리합니다.To be only used when calling through COM interop, and only parameters that are passed by reference are handled. 기본 바인더에서는 이 매개 변수를 처리하지 않습니다.The default binder does not process this parameter.
반환
(발견된 경우) 지정된 제네릭 매개 변수의 수, 인수 형식 및 한정자와 일치하는 공용 메서드를 나타내는 개체입니다. 그 밖의 경우에는 null
.An object representing the public method that matches the specified generic parameter count, argument types and modifiers, if found; otherwise, null
.
예외
name
이(가) null
인 경우name
is null
.
또는-or-
types
이(가) null
인 경우types
is null
.
또는-or-
types
배열의 요소 중 하나가 null
입니다.One of the elements in the types
array is null
.
genericParameterCount
가 음수입니다.genericParameterCount
is negative.
적용 대상
GetMethod(String, Type[])
지정된 인수 형식과 일치하는 매개 변수를 가진 지정된 public 메서드를 검색합니다.Searches for the specified public method whose parameters match the specified argument types.
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);
public System.Reflection.MethodInfo? GetMethod (string name, Type[] types);
public System.Reflection.MethodInfo GetMethod (string name, Type[] types);
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 메서드의 이름이 포함된 문자열입니다.The string containing the name of the public method to get.
- types
- Type[]
가져올 메서드에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.An array of Type objects representing the number, order, and type of the parameters for the method to get.
또는-or- 매개 변수를 사용하지 않는 메서드를 가져올 Type 개체(EmptyTypes 필드에서 제공됨)의 빈 배열입니다.An empty array of Type objects (as provided by the EmptyTypes field) to get a method that takes no parameters.
반환
지정된 인수 형식과 일치하는 매개 변수를 가진 public 메서드를 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null
을 반환합니다.An object representing the public method whose parameters match the specified argument types, if found; otherwise, null
.
구현
예외
지정된 이름 및 지정된 매개 변수를 사용하는 메서드가 두 개 이상입니다.More than one method is found with the specified name and specified parameters.
name
이(가) null
인 경우name
is null
.
또는-or-
types
이(가) null
인 경우types
is null
.
또는-or-
types
의 요소 중 하나가 null
입니다.One of the elements in types
is null
.
types
가 다차원 배열인 경우types
is multidimensional.
예제
다음 예에서는 다양 한 인수 형식을 지정 하 여의 특정 오버 로드 MethodA
를 찾습니다.The following example finds specific overloads of MethodA
, specifying a variety of argument types.
참고
이 Visual C# 2005Visual C# 2005 예제에는 /unsafe
컴파일러 옵션이 필요 합니다.The Visual C# 2005Visual C# 2005 example requires the /unsafe
compiler option.
using namespace System;
using namespace System::Reflection;
public ref class Program
{
public:
// Methods to get:
void MethodA(int i, int j) { }
void MethodA(array<int>^ iarry) { }
void MethodA(double *ip) { }
// Method that takes a managed reference parameter.
void MethodA(int% r) {}
};
int main()
{
MethodInfo^ mInfo;
// Get MethodA(int i, int j)
mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {int::typeid,int::typeid});
Console::WriteLine("Found method: {0}", mInfo );
// Get MethodA(array<int>^ iarry)
mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {int::typeid->MakeArrayType()});
Console::WriteLine("Found method: {0}", mInfo );
// Get MethodA(double *ip)
mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {double::typeid->MakePointerType()});
Console::WriteLine("Found method: {0}", mInfo );
// Get MethodA(int% r)
mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {int::typeid->MakeByRefType()});
// Display the method information.
Console::WriteLine("Found method: {0}", mInfo );
}
using System;
using System.Reflection;
class Program
{
// 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);
}
}
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
다음 예제에서는 MethodInfo Add
제네릭이 아닌 형식 ( ArrayList 클래스), 개방형 제네릭 형식 ( List<T> 클래스) 및 폐쇄형 제네릭 형식 (형식)의 메서드를 나타내는 개체를 검색 합니다 List(Of String)
.The following example retrieves MethodInfo objects that represent the Add
methods of a non-generic type (the ArrayList class), an open generic type (the List<T> class), and a closed generic type (the List(Of String)
type.
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)
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)
이 예제에서는 GetAddMethod
적절 한 개체를 검색 하는 메서드를 정의 합니다 MethodInfo .The example defines a GetAddMethod
method that retrieves the appropriate MethodInfo object. types
개방형 제네릭 형식에 대 한 인수를 제공 하기 위해 메서드를 호출 Type.GetGenericArguments 합니다.To provide the types
argument for an open generic type, it calls the Type.GetGenericArguments method. types
폐쇄형 제네릭 형식에 대 한 인수를 제공 하기 위해 속성의 값을 검색 Type.GenericTypeArguments 합니다.To provide the types
argument for a closed generic type, it retrieves the value of the Type.GenericTypeArguments property.
설명
검색은 대 name
/소문자를 구분 합니다.The search for name
is case-sensitive. 검색에는 public static 및 public instance 메서드가 포함 됩니다.The search includes public static and public instance methods.
참고
생성자와 메서드를 조회할 때 매개 변수를 생략할 수 없습니다.You cannot omit parameters when looking up constructors and methods. 를 호출할 때에만 매개 변수를 생략할 수 있습니다.You can only omit parameters when invoking.
현재 Type 이 생성 된 제네릭 형식을 나타내는 경우이 메서드는 MethodInfo 형식 매개 변수를 적절 한 형식 인수로 대체 한를 반환 합니다.If the current Type represents a constructed generic type, this method returns the MethodInfo with the type parameters replaced by the appropriate type arguments.
현재 Type 이 제네릭 형식 또는 제네릭 메서드 정의의 형식 매개 변수를 나타내는 경우이 메서드는 클래스 제약 조건의 메서드나 Object 클래스 제약 조건이 없는 경우의 메서드를 검색 합니다.If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint.
참고
name
매개 변수는 형식 인수를 포함할 수 없습니다.The name
parameter cannot include type arguments. 예를 들어 c # 코드는 GetMethod("MyGenericMethod<int>")
MyGenericMethod<int>
MyGenericMethod
형식의 제네릭 인수 하나를 포함 하는 라는 메서드가 아니라 텍스트 이름이 "" 인 메서드를 검색 합니다 int
.For example, the C# code GetMethod("MyGenericMethod<int>")
searches for a method with the text name "MyGenericMethod<int>
", rather than for a method named MyGenericMethod
that has one generic argument of type int
. 대신, GetMethod("MyGenericMethod")
배열에서 적절 한 매개 변수와 함께를 사용 types
합니다.Instead, use GetMethod("MyGenericMethod")
with the appropriate parameter in the types
array.
추가 정보
- MethodInfo
- DefaultBinder
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
적용 대상
GetMethod(String, Int32, Type[])
지정된 제네릭 매개 변수의 수 및 인수 형식과 매개 변수가 일치하는 지정된 공용 메서드를 검색합니다.Searches for the specified public method whose parameters match the specified generic parameter count and argument types.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types);
public System.Reflection.MethodInfo? GetMethod (string name, int genericParameterCount, Type[] types);
public System.Reflection.MethodInfo GetMethod (string name, int genericParameterCount, Type[] types);
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 메서드의 이름이 포함된 문자열입니다.The string containing the name of the public method to get.
- genericParameterCount
- Int32
메서드의 제네릭 형식 매개 변수의 수입니다.The number of generic type parameters of the method.
- types
- Type[]
가져올 메서드에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.An array of Type objects representing the number, order, and type of the parameters for the method to get.
또는-or-
매개 변수를 사용하지 않는 메서드를 가져올 Type 개체(EmptyTypes 필드에서 제공됨)의 빈 배열입니다.An empty array of Type objects (as provided by the EmptyTypes field) to get a method that takes no parameters.
반환
(발견된 경우) 지정 제네릭 매개 변수의 수 및 인수 형식과 매개 변수가 일치하는 공용 메서드를 나타내는 개체입니다. 그 밖의 경우에는 null
.An object representing the public method whose parameters match the specified generic parameter count and argument types, if found; otherwise, null
.
예외
name
이(가) null
인 경우name
is null
.
또는-or-
types
이(가) null
인 경우types
is null
.
또는-or-
types
배열의 요소 중 하나가 null
입니다.One of the elements in the types
array is null
.
genericParameterCount
가 음수입니다.genericParameterCount
is negative.
적용 대상
GetMethod(String, BindingFlags)
지정된 메서드를 지정된 바인딩 제약 조건으로 검색합니다.Searches for the specified method, using the specified binding constraints.
public:
virtual System::Reflection::MethodInfo ^ GetMethod(System::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);
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
가져올 메서드의 이름이 포함된 문자열입니다.The string containing the name of the method to get.
- bindingAttr
- BindingFlags
검색 방법을 지정하는 열거형 값의 비트 조합입니다.A bitwise combination of the enumeration values that specify how the search is conducted.
반환
지정된 요구 사항과 일치하는 메서드를 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null
을 반환합니다.An object representing the method that matches the specified requirements, if found; otherwise, null
.
구현
예외
지정한 이름을 가지며 지정된 바인딩 제약 조건과 일치하는 메서드가 둘 이상 발견되었습니다.More than one method is found with the specified name and matching the specified binding constraints.
name
이(가) null
인 경우name
is null
.
예제
다음 예제에서는 지정 된 바인딩 플래그와 일치 하는 메서드를 가져옵니다.The following example gets the method that matches the specified binding flags.
using namespace System;
using namespace System::Reflection;
public ref class Program
{
public:
// Method to get:
void MethodA() { }
};
int main()
{
// Get MethodA()
MethodInfo^ mInfo = Program::typeid->GetMethod("MethodA",
static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance));
Console::WriteLine("Found method: {0}", mInfo );
}
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",
BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine("Found method: {0}", 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 필터 플래그를 사용 하 여 검색에 포함할 메서드를 정의할 수 있습니다.The following BindingFlags filter flags can be used to define which methods to include in the search:
BindingFlags.Instance반환을 얻으려면 또는 중 하나를 지정 해야 합니다 BindingFlags.Static .You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.
BindingFlags.Public검색에 공용 메서드를 포함 하도록 지정 합니다.Specify BindingFlags.Public to include public methods in the search.
BindingFlags.NonPublic검색에 public이 아닌 메서드 (private, internal 및 protected 메서드)를 포함 하도록 지정 합니다.Specify BindingFlags.NonPublic to include non-public methods (that is, private, internal, and protected methods) in the search.
BindingFlags.FlattenHierarchy
public
계층의 정적 멤버를 포함 하도록 지정 합니다. 상속 된protected
private
클래스의 정적 멤버는 포함 되지 않습니다.Specify BindingFlags.FlattenHierarchy to includepublic
andprotected
static members up the hierarchy;private
static members in inherited classes are not included.
다음 BindingFlags 한정자 플래그를 사용 하 여 검색의 작동 방식을 변경할 수 있습니다.The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.IgnoreCase 의 대/소문자를 무시 하려면입니다
name
.BindingFlags.IgnoreCase to ignore the case ofname
.BindingFlags.DeclaredOnly 단순히 상속 된 메서드가 아니라에 선언 된 메서드만 검색 합니다 Type .BindingFlags.DeclaredOnly to search only the methods declared on the Type, not methods that were simply inherited.
자세한 내용은 System.Reflection.BindingFlags를 참조하세요.See System.Reflection.BindingFlags for more information.
메서드가 오버 로드 되 고 둘 이상의 오버 로드가 인수로 지정 된 제약 조건을 충족 하는 경우 bindingAttr
메서드는 예외를 throw AmbiguousMatchException 합니다.If a method is overloaded and more than one overload meets the constraints specified by the bindingAttr
argument, the method throws an AmbiguousMatchException exception. 다음 예제에서는 다음을 수행 하기 때문에 예외가 throw 됩니다.In the following example, an exception is thrown because:
형식에는
TestClass
메서드의 공용 인스턴스 오버 로드 두 개 (및)가 있습니다DisplayValue
DisplayValue(String)
DisplayValue(String, Object[])
.TheTestClass
type has two public instance overloads of theDisplayValue
method,DisplayValue(String)
andDisplayValue(String, Object[])
.형식에는
TestClass
메서드에 대 한 두 개의 공용 인스턴스 오버 로드가 있습니다Equals
. 그 중 하나 Object 는Equals(TestClass)
및에서 상속 됩니다Equals(Object)
.TheTestClass
type has two public instance overloads of theEquals
method, one of which is inherited from Object:Equals(TestClass)
andEquals(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 Example
{
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)
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)
다음 중 하나를 수행 하 여 특정 메서드를 검색할 수 있습니다.You can do one of the following to retrieve a specific method:
바인딩 제약 조건을 변경 합니다.Change the binding constraints. 이전 예제에서
Equals
형식으로 선언 되 고 상속 되지 않은 public 인스턴스 메서드를 검색 하는 것은 성공적으로 검색Equals(TestClass)
합니다.In the previous example, attempting to retrieve a public instanceEquals
method that is declared by the type and not inherited successfully retrievesEquals(TestClass)
.GetMethod메서드의 매개 변수 형식을 정의 하는 매개 변수를 포함 하는 메서드의 오버 로드를 호출
types
합니다.Call an overload of the GetMethod method that includes atypes
parameter which defines the types of the method's parameters.메서드를 호출 GetMethods(BindingFlags) 하 여 지정 된 바인딩 특성이 있는 형식에 속하는 모든 메서드를 포함 하는 배열을 검색 합니다.Call the GetMethods(BindingFlags) method to retrieve an array containing all of the methods belonging to a type that have the specified binding attributes. 그런 다음이를 반복 하 여 이라는 중복 메서드를 식별할 수 있습니다
name
.You can then iterate it to identify the duplicate methods namedname
. 이 방법은 이전 예제의 예외에 대 한 처리기에 설명 되어 AmbiguousMatchException 있습니다.This approach is illustrated in the previous example's handler for the AmbiguousMatchException exception.
현재 Type 이 생성 된 제네릭 형식을 나타내는 경우이 메서드는 MethodInfo 형식 매개 변수를 적절 한 형식 인수로 대체 한를 반환 합니다.If the current Type represents a constructed generic type, this method returns the MethodInfo with the type parameters replaced by the appropriate type arguments.
현재 Type 이 제네릭 형식 또는 제네릭 메서드 정의의 형식 매개 변수를 나타내는 경우이 메서드는 클래스 제약 조건의 메서드나 Object 클래스 제약 조건이 없는 경우의 메서드를 검색 합니다.If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint.
참고
제네릭 메서드의 경우에 형식 인수를 포함 하지 마십시오 name
.For generic methods, do not include the type arguments in name
. 예를 들어 c # 코드는 GetMember("MyMethod<int>")
MyMethod<int>
MyMethod
형식의 제네릭 인수 하나를 포함 하는 라는 메서드가 아니라 텍스트 이름이 "" 인 멤버를 검색 int
합니다.For example, the C# code GetMember("MyMethod<int>")
searches for a member with the text name "MyMethod<int>
", rather than for a method named MyMethod
that has one generic argument of type int
.
추가 정보
- MethodInfo
- String
- BindingFlags
- DefaultBinder
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
적용 대상
GetMethod(String)
지정된 이름의 public 메서드를 검색합니다.Searches for the public method with the specified name.
public:
System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
public:
virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
public System.Reflection.MethodInfo? GetMethod (string name);
public System.Reflection.MethodInfo GetMethod (string name);
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 메서드의 이름이 포함된 문자열입니다.The string containing the name of the public method to get.
반환
지정된 이름의 public 메서드를 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null
을 반환합니다.An object that represents the public method with the specified name, if found; otherwise, null
.
구현
예외
지정한 이름을 가진 메서드가 둘 이상 있습니다.More than one method is found with the specified name.
name
이(가) null
인 경우name
is null
.
예제
다음 예제에서는 라는 메서드를 가져옵니다 MethodA
.The following example gets a method named MethodA
.
using namespace System;
using namespace System::Reflection;
public ref class Program
{
public:
// Method to get:
void MethodA() { }
};
int main()
{
// Get MethodA()
MethodInfo^ mInfo = Program::typeid->GetMethod("MethodA");
Console::WriteLine("Found method: {0}", mInfo );
}
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);
}
}
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
/소문자를 구분 합니다.The search for name
is case-sensitive. 검색에는 public static 및 public instance 메서드가 포함 됩니다.The search includes public static and public instance methods.
메서드가 오버 로드 되 고 둘 이상의 public 메서드를 포함 하는 경우 GetMethod(String) 메서드는 예외를 throw AmbiguousMatchException 합니다.If a method is overloaded and has more than one public method, the GetMethod(String) method throws an AmbiguousMatchException exception. 다음 예제에서는 메서드의 공용 오버 로드가 둘 이상 있기 때문에 예외가 throw 됩니다 Int32.ToString .In the following example, an exception is thrown because there is more than one public overload of the Int32.ToString method. 반면, 메서드는를 Person.ToString
재정의 Object.ToString 하 여 오버 로드 되지 않으므로 메서드는 개체를 GetMethod(String) 검색할 수 MethodInfo 있습니다.On the other hand, because the Person.ToString
method overrides Object.ToString and therefore is not overloaded, the GetMethod(String) method is able to retrieve the MethodInfo object.
using System;
using System.Reflection;
public class Person
{
public String FirstName;
public String LastName;
public override String ToString()
{
return (FirstName + " " + LastName).Trim();
}
}
public class Example
{
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.
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.
다음 중 하나를 수행 하 여 특정 메서드를 검색할 수 있습니다.You can do one of the following to retrieve a specific method:
메서드를 호출 하 GetMethod(String, BindingFlags) 고
bindingAttr
메서드를 고유 하 게 식별 하는 인수를 지정 합니다.Call the GetMethod(String, BindingFlags) method and specify abindingAttr
argument that uniquely identifies the method. 예를 들어, 형식에 정적 및 인스턴스 오버 로드가 있으므로 예외가 throw 되는 경우의 인수를 지정할 수 있습니다bindingAttr
BindingFlags.InstanceOr
BindingFlags.Instance .For example, if the exception is thrown because a type has a static and an instance overload, you can specify abindingAttr
argument of BindingFlags.InstanceOr
BindingFlags.Instance.GetMethod메서드의 매개 변수 형식을 정의 하는 매개 변수를 포함 하는 메서드의 오버 로드를 호출
types
합니다.Call an overload of the GetMethod method that includes atypes
parameter which defines the types of the method's parameters.메서드를 호출 GetMethods() 하 여 형식에 속하는 모든 public 메서드를 포함 하는 배열을 검색 합니다.Call the GetMethods() method to retrieve an array containing all of the public methods belonging to a type. 그런 다음이를 반복 하 여 이라는 중복 메서드를 식별할 수 있습니다
name
.You can then iterate it to identify the duplicate methods namedname
.
현재 Type 이 생성 된 제네릭 형식을 나타내는 경우이 메서드는 MethodInfo 형식 매개 변수를 적절 한 형식 인수로 대체 한를 반환 합니다.If the current Type represents a constructed generic type, this method returns the MethodInfo with the type parameters replaced by the appropriate type arguments.
현재 Type 이 제네릭 형식 또는 제네릭 메서드 정의의 형식 매개 변수를 나타내는 경우이 메서드는 클래스 제약 조건의 메서드나 Object 클래스 제약 조건이 없는 경우의 메서드를 검색 합니다.If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint.
참고
제네릭 메서드의 경우에 형식 인수를 포함 하지 마십시오 name
.For generic methods, do not include the type arguments in name
. 예를 들어 c # 코드는 GetMember("MyMethod<int>")
MyMethod<int>
MyMethod
형식의 제네릭 인수 하나를 포함 하는 라는 메서드가 아니라 텍스트 이름이 "" 인 멤버를 검색 int
합니다.For example, the C# code GetMember("MyMethod<int>")
searches for a member with the text name "MyMethod<int>
", rather than for a method named MyMethod
that has one generic argument of type int
.
추가 정보
- MethodInfo
- String
- DefaultBinder
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()
적용 대상
GetMethod(String, Type[], ParameterModifier[])
지정된 인수 형식 및 한정자와 일치하는 매개 변수를 가진 지정된 public 메서드를 검색합니다.Searches for the specified public method whose parameters match the specified argument types and modifiers.
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);
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);
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 메서드의 이름이 포함된 문자열입니다.The string containing the name of the public method to get.
- types
- Type[]
가져올 메서드에 대한 매개 변수의 수, 차수, 형식 등을 나타내는 Type 개체 배열입니다.An array of Type objects representing the number, order, and type of the parameters for the method to get.
또는-or- 매개 변수를 사용하지 않는 메서드를 가져올 Type 개체(EmptyTypes 필드에서 제공됨)의 빈 배열입니다.An empty array of Type objects (as provided by the EmptyTypes field) to get a method that takes no parameters.
- modifiers
- ParameterModifier[]
ParameterModifier 배열에서 해당하는 요소와 연관된 특성을 나타내는 types
개체 배열입니다.An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types
array. COM interop를 통해 호출하는 경우에만 사용되며 참조에 의해 전달된 매개 변수만 처리합니다.To be only used when calling through COM interop, and only parameters that are passed by reference are handled. 기본 바인더에서는 이 매개 변수를 처리하지 않습니다.The default binder does not process this parameter.
반환
지정된 요구 사항과 일치하는 public 메서드를 나타내는 개체이며(있는 경우), 이러한 개체가 없으면 null
을 반환합니다.An object representing the public method that matches the specified requirements, if found; otherwise, null
.
구현
예외
지정된 이름 및 지정된 매개 변수를 사용하는 메서드가 두 개 이상입니다.More than one method is found with the specified name and specified parameters.
name
이(가) null
인 경우name
is null
.
또는-or-
types
이(가) null
인 경우types
is null
.
또는-or-
types
의 요소 중 하나가 null
입니다.One of the elements in types
is null
.
types
가 다차원 배열인 경우types
is multidimensional.
또는-or-
modifiers
가 다차원 배열인 경우modifiers
is multidimensional.
설명
기본 바인더는 ParameterModifier ( modifiers
매개 변수)를 처리 하지 않지만 추상 클래스를 사용 하 여 System.Reflection.Binder 처리 하는 사용자 지정 바인더를 작성할 수 있습니다 modifiers
.Although the default binder does not process ParameterModifier (the modifiers
parameter), you can use the abstract System.Reflection.Binder class to write a custom binder that does process modifiers
. ParameterModifier
는 COM interop를 통해 호출 하는 경우에만 사용 되며 참조로 전달 되는 매개 변수만 처리 합니다.ParameterModifier
is only used when calling through COM interop, and only parameters that are passed by reference are handled.
검색은 대 name
/소문자를 구분 합니다.The search for name
is case-sensitive. 검색에는 public static 및 public instance 메서드가 포함 됩니다.The search includes public static and public instance methods.
참고
생성자와 메서드를 조회할 때 매개 변수를 생략할 수 없습니다.You cannot omit parameters when looking up constructors and methods. 를 호출할 때에만 매개 변수를 생략할 수 있습니다.You can only omit parameters when invoking.
현재 Type 이 생성 된 제네릭 형식을 나타내는 경우이 메서드는 MethodInfo 형식 매개 변수를 적절 한 형식 인수로 대체 한를 반환 합니다.If the current Type represents a constructed generic type, this method returns the MethodInfo with the type parameters replaced by the appropriate type arguments.
현재 Type 이 제네릭 형식 또는 제네릭 메서드 정의의 형식 매개 변수를 나타내는 경우이 메서드는 클래스 제약 조건의 메서드나 Object 클래스 제약 조건이 없는 경우의 메서드를 검색 합니다.If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint.
참고
제네릭 메서드의 경우에 형식 인수를 포함 하지 마십시오 name
.For generic methods, do not include the type arguments in name
. 예를 들어 c # 코드는 GetMethod("MyMethod<int>")
MyMethod<int>
MyMethod
형식의 제네릭 인수 하나를 포함 하는 라는 메서드가 아니라 텍스트 이름이 "" 인 멤버를 검색 int
합니다.For example, the C# code GetMethod("MyMethod<int>")
searches for a member with the text name "MyMethod<int>
", rather than for a method named MyMethod
that has one generic argument of type int
. 대신, GetMethod("MyMethod")
배열에서 적절 한 매개 변수와 함께를 사용 types
합니다.Instead, use GetMethod("MyMethod")
with the appropriate parameter in the types
array.
추가 정보
- MethodInfo
- String
- DefaultBinder
- ParameterModifier
- GetMethodImpl(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])
- GetMethods()