InvokeMember calls a constructor member or a method member, gets or sets a property member, gets or sets a data field member, or gets or sets an element of an array member.
Note: |
|---|
You cannot use InvokeMember to invoke a generic method. |
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.
Although the default binder does not process ParameterModifier or CultureInfo (the modifiers and culture parameters), you can use the abstract System.Reflection..::.Binder class to write a custom binder that does process modifiers and culture. ParameterModifier is only used when calling through COM interop, and only parameters that are passed by reference are handled.
Each parameter in the namedParameters array gets the value in the corresponding element in the args array. If the length of args is greater than the length of namedParameters, the remaining argument values are passed in order.
The namedParameters array can be used to change the order of arguments in an input array. For example, given the method M(string a, int b) (M(ByVal a As String, ByVal b As Integer) in Visual Basic) and the input array { 42, "x" }, the input array can be passed unchanged to args if the array { "b", "a" } is supplied for namedParameters.
The following 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 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 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 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).
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 (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 Binder..::.BindToMethod method of the Binder class is responsible for selecting the method to be invoked. The default binder selects the most specific match.
InvokeMember can be used to invoke methods with parameters that have default values. To bind to these methods, Reflection requires BindingFlags..::.OptionalParamBinding to be specified. For a parameter that has a default value, you can either supply a different value, or supply Missing..::.Value to use the default value.
For example, consider a method such as MyMethod(int x, float y = 2.0). To invoke this method with only the first argument as MyMethod(4), pass one of the above binding flags and pass two arguments, namely, 4 for the first argument and Missing.Value for the second argument. Unless you use Missing.Value, you may not omit optional parameters with the Invoke method. If you must do so, use InvokeMember instead.
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 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"}, null, null, null);
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"}}, null, null, null);
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"}, null, null, null);
This will change string "z" in the array that F holds to string "b".
Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: This member throws a NotSupportedException on the .NET Compact Framework.