System.Reflection.MethodBase.Invoke Method

When overridden in a derived class, invokes the reflected method or constructor with the given parameters.

Syntax

public abstract object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, System.Globalization.CultureInfo culture)

Parameters

invokeAttr
A bitmask that is a combination of 0 or more bit flags from System.Reflection.BindingFlags. If binder is null, this parameter is assigned the value BindingFlags.Default; thus, whatever you pass in is ignored.
binder
An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects via reflection. If binder is null, the default binder is used.
parameters
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 null.
culture
An instance of CultureInfo used to govern the coercion of types. If this is null, 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.)
obj
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 null or an instance of the class that defines the constructor.
invokeAttr
A bitmask that is a combination of 0 or more bit flags from System.Reflection.BindingFlags. If binder is null, this parameter is assigned the value BindingFlags.Default; thus, whatever you pass in is ignored.
binder
An object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects via reflection. If binder is null, the default binder is used.
parameters
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 null.
culture
An instance of CultureInfo used to govern the coercion of types. If this is null, 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.)

Returns

An Object containing the return value of the invoked method, or null in the case of a constructor, or null 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.

Note:

Elements of the parameters array that represent parameters declared with the ref or out keyword may also be modified.

Exceptions

TypeReason
ArgumentExceptionThe types of the elements of parameters do not match the types of the parameters accepted by the constructor or method reflected by the current instance, under the constraints of the default binder.
System.Reflection.TargetExceptionThe constructor or method reflected by the current instance is non-static, and obj is null or is of a type that does not implement the member reflected by the current instance.
System.Reflection.TargetInvocationExceptionThe method reflected by the current instance threw an exception.
System.Reflection.TargetParameterCountException parameters.Length does not equal the number of parameters required by the contract of the constructor or method reflected by the current instance.
MemberAccessExceptionThe caller does not have permission to execute the method or constructor.
InvalidOperationExceptionThe type that declares the method is an open generic type. That is, Type.ContainsGenericParameters returns true for the declaring type.

Remarks

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 null. 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.

Note:

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 null. If a class constructor is invoked, the class is reinitialized; that is, all class initializers are executed. The return value is null.

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[0] = new Object("SomeString1") and parameters[1] = new Object("SomeString2").

If a parameter of the current method is a value type, and the corresponding argument in parameters is null, 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.

Note:

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.

Permissions

TypeReason
System.Security.Permissions.ReflectionPermissionRequires permission to invoke non-public members of loaded assemblies. See System.Security.Permissions.ReflectionPermissionFlag.MemberAccess.

Requirements

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0