Export (0) Print
Expand All

MethodBase.Invoke Method (Object, Object[])

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

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

[DebuggerStepThroughAttribute]
[DebuggerHiddenAttribute]
public Object Invoke(
	Object obj,
	Object[] parameters
)

Parameters

obj
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 null or an instance of the class that defines the constructor.
parameters
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, parameters should be null.
If the method or constructor represented by this instance takes a ref parameter (ByRef in Visual Basic), no special attribute is 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 null. For value-type elements, this value is 0, 0.0, or false, depending on the specific element type.

Return Value

Type: System.Object
The return value of the invoked method, or null in the case of a constructor.
Caution noteCaution:

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

ExceptionCondition
TargetException

The obj parameter is null and the method is not static.

-or-

The method is not declared or inherited by the class of obj.

-or-

A static constructor is invoked, and obj is neither null nor an instance of the class that declared the constructor.

ArgumentException

The elements of the parameters array do not match the signature of the method or constructor reflected by this instance.

TargetInvocationException

The invoked method or constructor throws an exception.

TargetParameterCountException

The parameters array does not have the correct number of arguments.

MethodAccessException

The method or constructor is not accessible.

InvalidOperationException

The type that declares the method is an open generic type. That is, the Type.ContainsGenericParameters property returns true for the declaring type.

In Silverlight, only accessible methods can be invoked using reflection.

Invoke(Object, Object[]) is a convenience overload that calls the Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) method overload, passing 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 MethodInfo object, pass null for obj.

NoteNote:

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.

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.

The following example demonstrates dynamic method lookup using reflection. Note that you cannot use the MethodInfo object from the base class to invoke the overridden method in the derived class, because late binding cannot resolve overrides.


using System;
using System.Reflection;

public class A
{
   public virtual int method() { return 0; }
}

public class B
{
   public virtual int method() { return 1; }
}

class Example
{
   public static int Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "Reflection.MethodInfo\n";
      A MyA = new A();
      B MyB = new B();

      // Get the Type and MethodInfo.
      Type MyTypea = typeof(A);
      MethodInfo Examplea = MyTypea.GetMethod("method");

      Type MyTypeb = typeof(B);
      MethodInfo Exampleb = MyTypeb.GetMethod("method");

      // Get and display the Invoke method.
      outputBlock.Text += "\nFirst method - " + MyTypea.FullName +
          " returns " + Examplea.Invoke(MyA, null);
      outputBlock.Text += "\nSecond method - " + MyTypeb.FullName +
          " returns " + Exampleb.Invoke(MyB, null);
      return 0;
   }
}

/* This code produces output similar to the following:

First method - A returns 0
Second method - B returns 1
 */


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

Show:
© 2014 Microsoft