IReflect.InvokeMember Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Invokes a specified member.
public:
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);
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
- name
- String
The name of the member to find.
- invokeAttr
- BindingFlags
One of the BindingFlags invocation attributes. 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.
- binder
- Binder
One of the BindingFlags bit flags. Implements Binder, containing properties related to this method.
- target
- Object
The object on which to invoke the specified member. This parameter is ignored for static members.
- args
- Object[]
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.
- modifiers
- ParameterModifier[]
An array of ParameterModifier objects. This array has the same length as the args
parameter, representing the invoked member's argument attributes in the metadata. A parameter can have the following attributes: pdIn
, pdOut
, pdRetval
, pdOptional
, and pdHasDefault
. These represent [In], [Out], [retval], [optional], and a default parameter, respectively. These attributes are used by various interoperability services.
- culture
- CultureInfo
An instance of CultureInfo used to govern the coercion of types. For example, culture
converts a string that represents 1000 to a Double value, since 1000 is represented differently by different cultures. If this parameter is null
, the CultureInfo for the current thread is used.
- namedParameters
- String[]
A string array of parameters.
The specified member.
More than one argument is specified for a field set
.
The field or property cannot be found.
The method cannot be found.
A private member is invoked without the necessary ReflectionPermission.
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]);
Console.WriteLine(result.ToString());
}
}
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) {})
Console.WriteLine(result.ToString())
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.
Notitie
The array of parameter modifiers passed to the InvokeMember method must contain a single parameter modifier. Only the first parameter modifier is considered when determining which argument needs to be passed by reference when exposed to COM.
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 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.
Product | Versies |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
Feedback over .NET
.NET is een opensourceproject. Selecteer een koppeling om feedback te geven: