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);
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 。 如果是 nullCultureInfo 則使用當前執行緒的 。 例如,這些資訊是正確 String 將代表1000 Double 的數字轉換成數值所必需的,因為不同文化對1000的表示方式不同。

傳回

Object A 包含所調用方法的回傳值。

例外狀況

VarArgs呼喚慣例不被支援。

元素 parameters 數量與動態方法中的參數數不相符。

一個 parameters 或多個元素的類型與動態方法對應參數的類型不符。

動態方法與模組相關聯,並非匿名託管,且建構時將 skipVisibility 設為 false,但動態方法會存取非 publicinternal(Visual Basic 中的 Friend)。

-或-

動態方法由匿名託管,並以 skipVisibility 設為 false建構,但它存取的成員並非 public

-或-

動態方法包含無法驗證的程式碼。 請參閱備註中的「驗證」部分。DynamicMethod

範例

以下程式碼範例使用精確綁定來調用動態方法,並採用美式英語文化。 此程式碼範例是本類別更大範例 DynamicMethod 的一部分。

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。 否則參數陣列中元素的數量、類型與順序應與動態方法的參數數量、類型及順序相同。

Note

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

此方法不直接要求權限,但依方法不同,啟用動態方法可能會產生安全需求。 例如,對於使用 restrictedSkipVisibility 參數設定為 false 的匿名託管動態方法,並沒有提出任何要求。 另一方面,如果你建立一個方法,設定 restrictedSkipVisibility 為 , true 讓它能存取目標組件的隱藏成員,該方法會要求目標組件的權限,並 ReflectionPermission 帶有 ReflectionPermissionFlag.MemberAccess 旗標。

適用於

另請參閱