Freigeben über


MethodBase.Invoke Method (Object, array<Object[])

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
<DebuggerStepThroughAttribute> _
<DebuggerHiddenAttribute> _
Public Function Invoke ( _
    obj As Object, _
    parameters As Object() _
) As Object
[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 nulla null reference (Nothing in Visual Basic) or an instance of the class that defines the constructor.
  • parameters
    Type: array<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 nulla null reference (Nothing in Visual Basic).
    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 nulla null reference (Nothing in Visual Basic). 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 nulla null reference (Nothing in Visual Basic) 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.

Exceptions

Exception Condition
TargetException

The obj parameter is nulla null reference (Nothing in Visual Basic) 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 nulla null reference (Nothing in Visual Basic) 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.

Remarks

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

Invoke(Object, array<Object[]) is a convenience overload that calls the Invoke(Object, BindingFlags, Binder, array<Object[], CultureInfo) method overload, passing Default for invokeAttr and nulla null reference (Nothing in Visual Basic) 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 nulla null reference (Nothing in Visual Basic) 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 nulla 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 nulla null reference (Nothing in Visual Basic).

If a parameter of the current method is a value type, and the corresponding argument in parameters is nulla null reference (Nothing in Visual Basic), the runtime passes a zero-initialized instance of the value type.

Examples

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.

Public Class A
   Public Overridable Function method() As Integer
      Return 0
   End Function
End Class

Public Class B
   Public Overridable Function method() As Integer
      Return 1
   End Function
End Class

Class Example

   Public Shared Function Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) As Integer
      outputBlock.Text &= "Reflection.MethodInfo" & vbCrLf
      outputBlock.Text &= vbCrLf
      Dim MyA As New A()
      Dim MyB As New B()

      'Get the Type and MethodInfo
      Dim MyTypea As Type = GetType(A)
      Dim Examplea As MethodInfo = MyTypea.GetMethod("method")

      Dim MyTypeb As Type = GetType(B)
      Dim Exampleb As MethodInfo = MyTypeb.GetMethod("method")

      'Get and display the Invoke method
      outputBlock.Text &= String.Format("First method - {0} returns {1}", MyTypea.FullName, _
          Examplea.Invoke(MyA, Nothing).ToString()) & vbCrLf
      outputBlock.Text &= String.Format("Second method - {0} returns {1}", MyTypeb.FullName, _
         Exampleb.Invoke(MyB, Nothing).ToString()) & vbCrLf
      Return 0
   End Function
End Class

' This code produces output similar to the following:
'
'First method - SilverlightApplication.A returns 0
'Second method - SilverlightApplication.B returns 1
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
 */

Version Information

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

Platforms

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