Invokes the specified member, using the specified binding constraints and matching the specified argument list.
- name
- The string containing the name of the constructor, method, property, or field member to invoke.
- invokeAttr
- A bitmask comprised of one or more System.Reflection.BindingFlags that specify how the search is conducted. The access can be one of the BindingFlags such as Public, NonPublic, Private, InvokeMethod, GetField, and so on. The type of lookup need not be specified. If the type of lookup is omitted, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static are used.
- binder
- An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.
- target
- The object on which to invoke the specified member.
- args
- An array containing the arguments to pass to the member to invoke.
An object representing the return value of the invoked member.
Type Reason ArgumentNullException name is null. ArgumentException args has more than one dimension.
-or-
invokeAttr is not a valid System.Reflection.BindingFlags value.
-or-
The member to be invoked is a constructor and System.Reflection.BindingFlags.CreateInstance is not specified in invokeAttr.
-or-
The member to be invoked is a method that is not a type initializer or instance constructor, and System.Reflection.BindingFlags.InvokeMethod is not specified in invokeAttr.
-or-
The member to be accessed is a field, and neither System.Reflection.BindingFlags.GetField nor System.Reflection.BindingFlags.SetField is specified in invokeAttr.
-or-
The member to be accessed is a property, and neither System.Reflection.BindingFlags.GetProperty nor System.Reflection.BindingFlags.SetProperty is specified in invokeAttr.
-or-
invokeAttr contains System.Reflection.BindingFlags.CreateInstance and at least one of System.Reflection.BindingFlags.InvokeMethod, System.Reflection.BindingFlags.GetField, System.Reflection.BindingFlags.SetField, System.Reflection.BindingFlags.GetProperty, or System.Reflection.BindingFlags.SetProperty.
-or-
invokeAttr contains both System.Reflection.BindingFlags.GetField and System.Reflection.BindingFlags.SetField.
-or-
invokeAttr contains both System.Reflection.BindingFlags.GetProperty and System.Reflection.BindingFlags.SetProperty.
-or-
invokeAttr contains System.Reflection.BindingFlags.InvokeMethod and at least one of System.Reflection.BindingFlags.SetField or System.Reflection.BindingFlags.SetProperty.
-or-
invokeAttr contains System.Reflection.BindingFlags.SetField and args has more than one element.
MissingFieldException A field or property matching the specified criteria was not found. MissingMethodException A method matching the specified criteria cannot be found.
-or-
The current instance object represents a type that contains open type parameters (that is,
Type.ContainsGenericParametersreturns
true).
MethodAccessException The requested member is non-public and the caller does not have the required permission. System.Reflection.TargetException The member matching the specified criteria cannot be invoked on target. System.Reflection.TargetInvocationException The member matching the specified criteria threw an exception. System.Reflection.AmbiguousMatchException More than one member matches the specified criteria.
You cannot use Type.InvokeMember(string, System.Reflection.BindingFlags, System.Reflection.Binder, object, Object[]) to invoke a generic method.
The following System.Reflection.BindingFlags filter flags can be used to define which members to include in the search:
Specify BindingFlags.Public to include public members in the search.
Specify BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
Specify BindingFlags.FlattenHierarchy to include static members up the hierarchy.
The following System.Reflection.BindingFlags modifier flags can be used to change how the search works:
BindingFlags.IgnoreCase to ignore the case of name.
BindingFlags.DeclaredOnly to search only the members declared on the Type, not members that were simply inherited.
The following System.Reflection.BindingFlags invocation flags can be used to denote what action to take with the member:
CreateInstance to invoke a constructor. name is ignored. Not valid with other invocation flags.
InvokeMethod to invoke a method, but not a constructor or a type initializer. Not valid with SetField or SetProperty. If InvokeMethod is specified by itself, BindingFlags.Public, BindingFlags.Instance, and BindingFlags.Static are automatically included.
GetField to get the value of a field. Not valid with SetField.
SetField to set the value of a field. Not valid with GetField.
GetProperty to get a property. Not valid with SetProperty.
SetProperty to set a property. Not valid with GetProperty.
See System.Reflection.BindingFlags for more information.
A method will be invoked if both of the following conditions are true:
The number of parameters in the method declaration equals the number of arguments in the args array (unless default arguments are defined on the member and BindingFlags.OptionalParamBinding is specified).
The type of each argument can be converted by the binder to the type of the parameter.
The binder will find all of the matching methods. These methods are found based upon the type of binding requested (System.Reflection.BindingFlags values InvokeMethod, GetProperty, 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. Accessibility is checked at that point. The search may control which set of methods are searched based upon the accessibility attribute associated with the method. The System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags, System.Reflection.MethodBase[], Object[]@, System.Reflection.ParameterModifier[], System.Globalization.CultureInfo, String[], Object@) method of the System.Reflection.Binder class is responsible for selecting 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 System.Reflection whenever the code is fully trusted.
You can use Type.InvokeMember to set a field to a particular value by specifying System.Reflection.BindingFlags.SetField. For example, if you want to set a public instance field named F on class C, and F is a String, you can use code such as:
typeof(C).InvokeMember("F", BindingFlags.SetField, null, c, new Object[] {"strings new value"});
If F is a String[], you can use code such as:
typeof(C).InvokeMember("F", BindingFlags.SetField, null, c, new Object[] {new String[]{"a","z","c","d"}});
which will initialize the field F to this new array. You can also use Type.InvokeMember to set a position in an array by supplying the index of the value and then the next value by using code such as the following:
typeof(C).InvokeMember("F", BindingFlags.SetField, null, c, new Object[] {1, "b"});
This will change string "z" in the array that F holds to string "b".
When you invoke an IDispatch member, you can specify the DispID instead of the member name, using the string format "[DispID=##]". For example, if the DispID of MyComMethod is 3, you can specify the string "[DispID=3]" instead of "MyComMethod". Invoking a member by DispID is faster than looking up the member by name. In complex aggregation scenarios, the DispID is sometimes the only way to invoke the desired member.
Starting with the net_v20sp1_long, this method can be used to access non-public members if the caller has been granted System.Security.Permissions.ReflectionPermission with the System.Security.Permissions.ReflectionPermissionFlag.RestrictedMemberAccess flag and if the grant set of the non-public members is restricted to the caller’s grant set, or a subset thereof. (See Security Considerations for Reflection.)
To use this functionality, your application should target the net_v35_long or later.
Type | Reason |
---|---|
System.Security.Permissions.ReflectionPermission | Requires permission to retrieve information on non-public members of types in loaded assemblies. See System.Security.Permissions.ReflectionPermissionFlag.TypeInformation. |