IReflect.InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]) 方法


叫用指定成員。Invokes a specified member.

 System::Object ^ InvokeMember(System::String ^ name, System::Reflection::BindingFlags invokeAttr, System::Reflection::Binder ^ binder, System::Object ^ target, cli::array <System::Object ^> ^ args, cli::array <System::Reflection::ParameterModifier> ^ modifiers, System::Globalization::CultureInfo ^ culture, cli::array <System::String ^> ^ namedParameters);
public object InvokeMember (string name, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object target, object[] args, System.Reflection.ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, string[] namedParameters);
abstract member InvokeMember : string * System.Reflection.BindingFlags * System.Reflection.Binder * obj * obj[] * System.Reflection.ParameterModifier[] * System.Globalization.CultureInfo * string[] -> obj



要尋找的成員名稱。The name of the member to find.


其中一個 BindingFlags 引動過程屬性。One of the BindingFlags invocation attributes. invokeAttr 參數可能是建構函式、方法、屬性或欄位。The invokeAttr parameter may be a constructor, method, property, or field. 您必須指定適當的引動過程屬性。A suitable invocation attribute must be specified. 藉由將空字串 ("") 傳遞為成員名稱的方式,叫用類別的預設成員。Invoke the default member of a class by passing the empty string ("") as the name of the member.


其中一個 BindingFlags 位元旗標。One of the BindingFlags bit flags. 實作 Binder,包含這個方法的相關屬性。Implements Binder, containing properties related to this method.


要在其上叫用指定成員的物件。The object on which to invoke the specified member. 對於靜態成員來說,會忽略這個參數。This parameter is ignored for static members.


物件陣列,包含要叫用的成員參數的數目、順序和型別。An array of objects that contains the number, order, and type of the parameters of the member to be invoked. 如果沒有參數,則這是空陣列。This is an empty array if there are no parameters.


ParameterModifier 物件的陣列。An array of ParameterModifier objects. 這個陣列和 args 參數具有相同的長度,並且表示中繼資料 (Metadata) 中被叫用成員的引數屬性。This array has the same length as the args parameter, representing the invoked member's argument attributes in the metadata. 參數可以具有下列屬性:pdInpdOutpdRetvalpdOptionalpdHasDefaultA parameter can have the following attributes: pdIn, pdOut, pdRetval, pdOptional, and pdHasDefault. 這些分別代表 [In]、[Out]、[retval]、[optional] 和預設參數。These represent [In], [Out], [retval], [optional], and a default parameter, respectively. 這些屬性是由各種互通性 (Interoperability) 服務所使用。These attributes are used by various interoperability services.


CultureInfo 的執行個體,用於管理類型的強制型轉。An instance of CultureInfo used to govern the coercion of types. 例如,culture 會將表示 1000 的 String 轉換為 Double 值,因為 1000 在不同文化特性 (Culture) 中有不同的表示方式。For example, culture converts a String that represents 1000 to a Double value, since 1000 is represented differently by different cultures. 如果這個參數是 null,則會使用目前執行緒的 CultureInfoIf this parameter is null, the CultureInfo for the current thread is used.


參數的 String 陣列。A String array of parameters.


指定成員。The specified member.


一個以上的引數會指定給欄位 setMore than one argument is specified for a field set.

找不到欄位或屬性。The field or property cannot be found.

找不到方法。The method cannot be found.

叫用私用 (Private) 成員,而不使用必要的 ReflectionPermissionA private member is invoked without the necessary ReflectionPermission.


下列範例會取得Now屬性的值。The following example obtains the value of the Now property.

#using <System.DLL>

using namespace System;
using namespace System::Reflection;

#define NULL 0
void main()
   Type^ tDate = Type::GetType( L"System.DateTime" );
   Object^ result = tDate->InvokeMember( L"Now", BindingFlags::GetProperty, nullptr, NULL, gcnew array<Object^>(0) );
   Console::WriteLine( result->ToString() );

using System;
using System.Reflection;

public class MainClass 
    public static void Main(string[] args)
        Type tDate = typeof(System.DateTime);
        Object result = tDate.InvokeMember("Now", 
            BindingFlags.GetProperty, null, null, new Object[0]);
Imports System.Reflection

Public Class MainClass
    Public Overloads Shared Sub Main(ByVal args() As String)
        Dim tDate As Type = GetType(System.DateTime)
        Dim result As [Object] = tDate.InvokeMember("Now", _
            BindingFlags.GetProperty, Nothing, Nothing, New [Object](-1) {})
    End Sub
End Class


根據指定繫結器和引動過程屬性的條件約束,所要叫用的方法必須可存取,並提供與指定引數清單最特定的符合項目。The method that is to be invoked must be accessible and provide the most specific match with the specified argument list, under the constraints of the specified binder and invocation attributes.

如果方法宣告中的參數數目等於指定引數清單中的引數數目, 而且每個引數的類型可以由系結器轉換成參數的類型, 則會叫用方法。A method is invoked if the number of parameters in the method declaration equals the number of arguments in the specified argument list, and the type of each argument can be converted by the binder to the type of the parameter.


傳遞給InvokeMember方法的參數修飾詞陣列必須包含單一參數修飾詞。The array of parameter modifiers passed to the InvokeMember method must contain a single parameter modifier. 當您在對 COM 公開時, 判斷需要以傳址方式傳遞的引數時, 只會考慮第一個參數修飾詞。Only the first parameter modifier is considered when determining which argument needs to be passed by reference when exposed to COM.

系結器會根據要求的系結類型 (BindingFlags.InvokeMethodGetProperties等等), 尋找所有相符的方法。The binder finds all matching methods, in accordance with the type of binding requested (BindingFlags.InvokeMethod, GetProperties, and so on). 一組方法會依名稱、引數數目和系結器中定義的一組搜尋修飾詞進行篩選。The set of methods is filtered by the name, number of arguments, and a set of search modifiers defined in the binder. 選取方法之後, 就會叫用它, 並在該時間點檢查存取範圍。After the method is selected, it is invoked, and accessibility is checked at that point. 搜尋可以根據與方法相關聯的存取範圍屬性, 控制要搜尋哪一組方法。The search may control which set of methods is searched based upon the accessibility attribute associated with the method. BindToMethod選取要叫用的方法。BindToMethod selects the method to be invoked. 預設系結器會選取最特定的相符項。The default binder selects the most specific match.

完全信任的程式碼會忽略存取限制。Access restrictions are ignored for fully trusted code. 也就是, 只要程式碼受到完全信任, 就可以透過反映來存取及叫用私用的函式、方法、欄位和屬性。That is, private constructors, methods, fields, and properties can be accessed and invoked through reflection whenever the code is fully trusted.