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。 例如,若要將代表 1000 的 String 正確轉換成 Double 值,需要這項資訊,因為 1000 的表示方式會因不同文化特性而有所不同。

傳回

Object,其中包含已叫用之方法的傳回值。

例外狀況

不支援 VarArgs 呼叫慣例。

parameters 中的項目數不符合動態方法中的參數數目。

parameters 的一或多個項目類型不符合動態方法的對應參數類型。

動態方法已與模組建立關聯、不是匿名裝載,而且建構時已將 skipVisibility 設定為 false,但動態方法所存取的成員卻不是 publicinternal (在 Visual Basic 中為 Friend)。

-或-

動態方法已匿名裝載,而且建構已將 skipVisibility 設定為 false,但其所存取的成員不是 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)

備註

除了列出的例外狀況之外,呼叫程式代碼也應該準備好攔截動態方法擲回的任何例外狀況。

使用 方法所 CreateDelegate 建立的委派執行動態方法,比使用 Invoke 方法執行動態方法更有效率。

Invoke呼叫 方法或 CreateDelegate 方法會完成動態方法。 會忽略任何進一步嘗試改變動態方法,例如修改參數定義或發出更多 Microsoft 中繼語言 (MSIL) ;不會擲回例外狀況。

所有動態方法都是靜態的,因此一 obj 律會忽略 參數。 若要將動態方法視為實例方法,請使用 CreateDelegate(Type, Object) 採用物件實例的多載。

如果動態方法沒有參數,則的值 parameters 應該是 null。 否則,參數陣列中元素的數目、類型和順序應該與動態方法的參數數目、類型和順序相同。

注意

此方法多載是由 Invoke(Object, Object[]) 繼承自 MethodBase 類別的方法多載所呼叫,因此上述備註適用於這兩個多載。

此方法不直接要求許可權,但叫用動態方法可能會導致安全性需求,視方法而定。 例如,不會對匿名裝載的動態方法提出任何要求,這些方法會使用 restrictedSkipVisibility 設定為 false的參數來建立。 另一方面,如果您建立設定為 的方法restrictedSkipVisibilitytrue,使其可以存取目標元件的隱藏成員,此方法將會導致要求目標元件的許可權加上 ReflectionPermissionReflectionPermissionFlag.MemberAccess 旗標。

注意

在 .NET Framework 2.0 之前,這個方法需要 ReflectionPermissionMemberAccess標。

適用於

另請參閱