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


调用指定的成员。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
Public Function InvokeMember (name As String, invokeAttr As BindingFlags, binder As Binder, target As Object, args As Object(), modifiers As ParameterModifier(), culture As CultureInfo, namedParameters As String()) As Object



要查找的成员的名称。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 参数具有相同的长度。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. 这些属性由不同的互操作性服务使用。These attributes are used by various interoperability services.


用于控制类型强制的 CultureInfo 的实例。An instance of CultureInfo used to govern the coercion of types. 例如,culture 将表示 1000 的字符串转换为 Double 值,因为不同的区域性以不同的方式表示 1000。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.


参数的字符串数组。A string array of parameters.



指定的成员。The specified member.


为字段 set 指定了多个参数。More than one argument is specified for a field set.

找不到此字段或属性。The field or property cannot be found.

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

在没有所需 ReflectionPermission 的情况下调用私有成员。A 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.

Applies to

See also