Invoke Method (Object, Object[])
Collapse the table of content
Expand the table of content

MethodBase.Invoke Method (Object, Object())

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

'Declaration
Public Function Invoke ( _
	obj As Object, _
	parameters As Object() _
) As Object

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

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


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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft