MethodBase.Invoke Method (Object, BindingFlags, Binder, Object, CultureInfo)
When overridden in a derived class, invokes the reflected method or constructor with the given parameters.
Assembly: mscorlib (in mscorlib.dll)
abstract Invoke : obj:Object * invokeAttr:BindingFlags * binder:Binder * parameters:Object * culture:CultureInfo -> Object
- Type: System.Object
The object on which to invoke the method or constructor. If a method is static, this argument is ignored. If a constructor is static, this argument must be a null reference (Nothing in Visual Basic) or an instance of the class that defines the constructor.
- Type: System.Reflection.BindingFlags
A bitmask that is a combination of 0 or more bit flags from BindingFlags. If binder is a null reference (Nothing in Visual Basic), this parameter is assigned the value Default; thus, whatever you pass in is ignored.
- Type: System.Reflection.Binder
An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects via reflection. If binder is a null reference (Nothing in Visual Basic), the default binder is used.
- Type: System.Object
An argument list for the invoked method or constructor. This is an array of objects with the same number, order, and type as the parameters of the method or constructor to be invoked. If there are no parameters, this should be a null reference (Nothing in Visual Basic).
If the method or constructor represented by this instance takes a ByRef parameter, there is no special attribute required for that parameter in order to invoke the method or constructor using this function. Any object in this array that is not explicitly initialized with a value will contain the default value for that object type. For reference-type elements, this value is a null reference (Nothing in Visual Basic). For value-type elements, this value is 0, 0.0, or false, depending on the specific element type.
- Type: System.Globalization.CultureInfo
An instance of CultureInfo used to govern the coercion of types. If this is a null reference (Nothing in Visual Basic), the CultureInfo for the current thread is used. (This is necessary to convert a String that represents 1000 to a Double value, for example, since 1000 is represented differently by different cultures.)
Return ValueType: System.Object
An Object containing the return value of the invoked method, or a null reference (Nothing in Visual Basic) in the case of a constructor, or a null reference (Nothing in Visual Basic) if the method's return type is void. Before calling the method or constructor, Invoke checks to see if the user has access permission and verifies that the parameters are valid.
Elements of the parameters array that represent parameters declared with the ref or out keyword may also be modified.
Implements_MethodBase.Invoke(Object, BindingFlags, Binder, Object, CultureInfo)
The obj parameter is a null reference (Nothing in Visual Basic) and the method is not static.
The method is not declared or inherited by the class of obj.
A static constructor is invoked, and obj is neither a null reference (Nothing in Visual Basic) nor an instance of the class that declared the constructor.
The type of the parameters parameter does not match the signature of the method or constructor reflected by this instance.
The parameters array does not have the correct number of arguments.
The invoked method or constructor throws an exception.
The caller does not have permission to execute the method or constructor that is represented by the current instance.
The type that declares the method is an open generic type. That is, the Type.ContainsGenericParameters property returns true for the declaring type.
Dynamically invokes the method reflected by this instance on obj, and passes along the specified parameters. If the method is static, the obj parameter is ignored. For non-static methods, obj should be an instance of a class that inherits or declares the method and must be the same type as this class. If the method has no parameters, the value of parameters should be a null reference (Nothing in Visual Basic). Otherwise, the number, type, and order of elements in parameters should be identical to the number, type, and order of parameters for the method reflected by this instance.
You may not omit optional parameters in calls to Invoke. To invoke a method omitting optional parameters, you should call Type.InvokeMember instead.
If this method overload is used to invoke an instance constructor, the object supplied for obj is reinitialized; that is, all instance initializers are executed. The return value is a null reference (Nothing in Visual Basic). If a class constructor is invoked, the class is reinitialized; that is, all class initializers are executed. The return value is a null reference (Nothing in Visual Basic).
For pass-by-value primitive parameters, normal widening is performed (Int16 -> Int32, for example). For pass-by-value reference parameters, normal reference widening is allowed (derived class to base class, and base class to interface type). However, for pass-by-reference primitive parameters, the types must match exactly. For pass-by-reference reference parameters, the normal widening still applies.
For example, if the method reflected by this instance is declared as public boolean Compare(String a, String b), then parameters should be an array of Objects with length 2 such that parameters = new Object("SomeString1") and parameters = new Object("SomeString2").
If a parameter of the current method is a value type, and the corresponding argument in parameters is a null reference (Nothing in Visual Basic), the runtime passes a zero-initialized instance of the value type.
Reflection uses dynamic method lookup when invoking virtual methods. For example, suppose that class B inherits from class A and both implement a virtual method named M. Now suppose that you have a MethodInfo object that represents M on class A. If you use the Invoke method to invoke M on an object of type B, then reflection will use the implementation given by class B. Even if the object of type B is cast to A, the implementation given by class B is used (see code sample below).
On the other hand, if the method is non-virtual, then reflection will use the implementation given by the type from which the MethodInfo was obtained, regardless of the type of the object passed as the target.
Access restrictions are ignored for fully trusted code. That is, private constructors, methods, fields, and properties can be accessed and invoked via reflection whenever the code is fully trusted.
If the invoked method throws an exception, TargetInvocationException.GetException returns the exception. This implementation throws a NotSupportedException.
Starting with the .NET Framework 2.0 Service Pack 1, this method can be used to access non-public members if the caller has been granted ReflectionPermission with the 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 Framework 3.5 or later.
The following example demonstrates all members of the System.Reflection.Binder class using an overload of Type.InvokeMember. The private method CanConvertFrom finds compatible types for a given type. For another example of invoking members in a custom binding scenario, see Dynamically Loading and Using Types.
for accessing non-public members when the grant set of the non-public members is restricted to the caller's grant set, or a subset thereof. Associated enumeration: ReflectionPermissionFlag.RestrictedMemberAccess
for accessing non-public members regardless of their grant set. Associated enumeration: ReflectionPermissionFlag.MemberAccess
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)