System.Reflection.MethodBase.Invoke Method

Invokes the method or constructor represented by the current instance, using the specified parameters.

Syntax

[System.Diagnostics.DebuggerStepThrough]
public object Invoke (object obj, object[] parameters)

Parameters

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, parameters should be null.
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.
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, parameters should be null.

Returns

An object containing the return value of the invoked method, or null in the case of a constructor.

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 constructor or 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 member 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

This is a convenience method that calls the MethodBase.Invoke(object, BindingFlags, Binder, Object[], System.Globalization.CultureInfo) method overload, passing BindingFlags.Default for invokeAttr and null for binder and culture.

If the invoked method throws an exception, the Exception.GetBaseException method returns the exception.

To invoke a static method using its System.Reflection.MethodInfo object, pass null for obj.

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.

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.

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.

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