DynamicMethod.Invoke 메서드

정의

지정된 문화권 정보를 사용하고 지정된 바인더의 제약 조건에 따라, 지정된 매개 변수를 사용하여 동적 메서드를 호출합니다.

public:
 override System::Object ^ Invoke(System::Object ^ obj, System::Reflection::BindingFlags invokeAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ parameters, System::Globalization::CultureInfo ^ culture);
public override object? Invoke (object? obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? parameters, System.Globalization.CultureInfo? culture);
public override object Invoke (object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture);
override this.Invoke : obj * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo -> obj
Public Overrides Function Invoke (obj As Object, invokeAttr As BindingFlags, binder As Binder, parameters As Object(), culture As CultureInfo) As Object

매개 변수

obj
Object

이 매개 변수는 정적이기 때문에 동적 메서드에 대해 무시됩니다. null을 지정합니다.

invokeAttr
BindingFlags

BindingFlags 값의 비트 조합입니다.

binder
Binder

리플렉션을 통해 바인딩, 인수 형식의 강제 변환, 멤버 호출 및 Binder 개체 검색을 사용할 수 있도록 하는 MemberInfo 개체입니다. bindernull이면 기본 바인더가 사용됩니다. 자세한 내용은 Binder를 참조하세요.

parameters
Object[]

인수 목록입니다. 이것은 호출된 메서드의 매개 변수와 같은 개수, 순서 및 형식의 인수 배열입니다. 매개 변수가 없는 경우에는 이 매개 변수가 null이어야 합니다.

culture
CultureInfo

형식의 강제 변환을 제어하는 데 사용되는 CultureInfo 의 인스턴스입니다. 이 매개 변수가 null이면 현재 스레드에 대한 CultureInfo가 사용됩니다. 예를 들어 1,000은 각 문화권마다 다르게 표현되므로 1,000을 나타내는 StringDouble 값으로 올바르게 변환하기 위해 이 정보가 필요합니다.

반환

Object

호출된 메서드의 반환 값을 포함하는 Object 입니다.

예외

VarArgs 호출 규칙은 지원되지 않습니다.

parameters에 있는 요소의 수가 동적 메서드에 있는 매개 변수의 수와 일치하지 않는 경우

parameters에 있는 하나 이상의 요소 형식이 동적 메서드에 있는 해당 매개 변수의 형식과 일치하지 않는 경우

동적 메서드를 모듈과 연결했고 익명으로 호스팅하지 않았으며 이를 생성할 때 skipVisibilityfalse로 설정했지만 이 동적 메서드에서 public 또는 internal(Visual Basic의 경우 Friend)이 아닌 멤버에 액세스하는 경우

또는 동적 메서드를 익명으로 호스팅했고 이를 생성할 때 skipVisibilityfalse로 설정했지만 이 동적 메서드에서 public이 아닌 멤버에 액세스하는 경우

또는 동적 메서드는 확인할 수 있는 코드를 포함합니다. DynamicMethod에 대한 설명에서 "확인" 섹션을 참조하세요.

예제

다음 코드 예제에서는 US-English 문화권을 사용하여 정확한 바인딩을 사용하여 동적 메서드를 호출합니다. 이 코드 예제는에 대해 제공 된 큰 예제의 일부는 DynamicMethod 클래스입니다.

Console::WriteLine("\r\nUse the Invoke method to execute the dynamic method:");
// Create an array of arguments to use with the Invoke method.
array<Object^>^ invokeArgs = { "\r\nHello, World!", 42 };
// Invoke the dynamic method using the arguments. This is much
// slower than using the delegate, because you must create an
// array to contain the arguments, and value-type arguments
// must be boxed.
Object^ objRet = hello->Invoke(nullptr, BindingFlags::ExactBinding, nullptr, invokeArgs, gcnew CultureInfo("en-us"));
Console::WriteLine("hello.Invoke returned: " + objRet);
Console.WriteLine("\r\nUse the Invoke method to execute the dynamic method:");
// Create an array of arguments to use with the Invoke method.
object[] invokeArgs = {"\r\nHello, World!", 42};
// Invoke the dynamic method using the arguments. This is much
// slower than using the delegate, because you must create an
// array to contain the arguments, and value-type arguments
// must be boxed.
object objRet = hello.Invoke(null, BindingFlags.ExactBinding, null, invokeArgs, new CultureInfo("en-us"));
Console.WriteLine("hello.Invoke returned: " + objRet);
Console.WriteLine(vbCrLf & "Use the Invoke method to execute the dynamic method:")
' Create an array of arguments to use with the Invoke method.
Dim invokeArgs() As Object = {vbCrLf & "Hello, World!", 42}
' Invoke the dynamic method using the arguments. This is much
' slower than using the delegate, because you must create an
' array to contain the arguments, and value-type arguments
' must be boxed.
Dim objRet As Object = hello.Invoke(Nothing, _
    BindingFlags.ExactBinding, Nothing, invokeArgs, _
    New CultureInfo("en-us"))
Console.WriteLine("hello.Invoke returned: {0}", objRet)

설명

나열된 예외 외에도 호출 코드는 동적 메서드에서 throw된 모든 예외를 catch할 수 있도록 준비되어야 합니다.

메서드에서 만든 대리자를 사용하여 동적 메서드를 CreateDelegate 실행하는 것이 메서드를 Invoke 사용하여 실행하는 것보다 더 효율적입니다.

Invoke 메서드 또는 메서드를 호출하면 CreateDelegate 동적 메서드가 완료됩니다. 매개 변수 정의를 수정하거나 MSIL(Microsoft 중간 언어)을 더 내보내는 등 동적 메서드를 변경하려는 추가 시도는 무시됩니다. 예외가 throw되지 않습니다.

모든 동적 메서드는 정적이므로 obj 매개 변수는 항상 무시됩니다. 동적 메서드를 인스턴스 메서드인 것처럼 처리하려면 개체 인스턴스를 CreateDelegate(Type, Object) 사용하는 오버로드를 사용합니다.

동적 메서드에 매개 변수가 없으면 값 parameters 은 .이어야 null합니다. 그렇지 않으면 매개 변수 배열의 요소 수, 형식 및 순서가 동적 메서드의 매개 변수 수, 형식 및 순서와 동일해야 합니다.

참고

이 메서드 오버로드는 클래스에서 상속된 메서드 오버로드에 MethodBase 의해 Invoke(Object, Object[]) 호출되므로 앞의 설명이 두 오버로드 모두에 적용됩니다.

이 메서드는 사용 권한을 직접 요구하지 않지만 동적 메서드를 호출하면 메서드에 따라 보안 요구가 발생할 수 있습니다. 예를 들어 매개 변수를 설정false하여 만든 익명으로 호스트된 동적 메서드에 restrictedSkipVisibility 대한 요구는 없습니다. 반면, 대상 어셈블리의 숨겨진 멤버에 액세스할 수 있도록 설정된 true 메서드 restrictedSkipVisibility 를 만들면 대상 어셈블리 ReflectionPermission 의 사용 권한과 플래그에 대한 요구가 ReflectionPermissionFlag.MemberAccess 발생합니다.

참고

2.0을 .NET Framework 전에 이 메서드는 플래그와 MemberAccess 함께 필요합니다ReflectionPermission.

적용 대상

추가 정보