This documentation is archived and is not being maintained.

MethodBase.Invoke Method (Object, Object[])

.NET Framework 1.1

Invokes the underlying method or constructor represented by this MethodInfo object with the specified parameters.

[Visual Basic]
Overloads Public Function Invoke( _
   ByVal obj As Object, _
   ByVal parameters() As Object _
) As Object
[C#]
public object Invoke(
 object obj,
 object[] parameters
);
[C++]
public: Object* Invoke(
 Object* obj,
 Object* parameters __gc[]
);
[JScript]
public function Invoke(
   obj : Object,
 parameters : Object[]
) : Object;

Parameters

obj
The instance that created this method.
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 a null reference (Nothing in Visual Basic).

If the method or constructor represented by this instance takes a ByRef parameter, there is no special attribute 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 a null reference (Nothing). For value-type elements, this value is 0, 0.0, or false, depending on the specific element type.

Return Value

An Object containing the return value of the invoked method, or a re-initialized object in the case of a constructor.

Exceptions

Exception Type Condition
TargetException The obj parameter is a 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.

ArgumentException The type of the parameters parameter does 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 caller does not have permission to execute the constructor.

Remarks

Use this method to invoke methods with parameters that have default values. To bind to these methods, Reflection requires one of the binding flags DefaultValueBinding, DefaultValueChangeType, or DefaultValueFull to be specified explicitly. This is a requirement even in those cases where a value is passed for a parameter that has a default value.

For example, consider a method such as MyMethod(int x, float y = 2.0). To invoke this method with only the first argument as MyMethod(4), pass one of the above binding flags and pass two arguments, namely, 4 for the first argument and Missing.Value for the second argument. Unless you use Missing.Value, you may not omit optional parameters with the Invoke method. If you must do so, use InvokeMember instead.

This is a convenience method that calls the following Invoke method, passing a null reference (Nothing in Visual Basic) in the other parameters. If the invoked method throws an exception, GetBaseException returns the exception.

To invoke a static method using its MethodInfo object, the first parameter should be a null reference (Nothing), as shown in the following call:

Object myReturnValue = myMethodInfo.Invoke(null, myParametersArray);

Example

The following samples demonstrate dynamic method lookup using reflection. Note that all of the invocations return 1, because the method in classes A and B is virtual (Overridable in Visual Basic) .

[Visual Basic] 
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 Mymethodinfo

    Public Shared Function Main() As Integer
        Console.WriteLine("Reflection.MethodInfo")
        Console.WriteLine()
        Dim MyA As New A()
        Dim MyB As New B()

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

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

        'Get and display the Invoke method
        Console.WriteLine("First method - {0} returns {1}", MyTypea.FullName, _
            Mymethodinfoa.Invoke(MyA, Nothing).ToString())
        Console.WriteLine("Second method - {0} returns {1}", MyTypeb.FullName, _
           Mymethodinfob.Invoke(MyB, Nothing).ToString())
        Return 0
    End Function
End Class

[C#] 
using System;
using System.Reflection;
using System.Windows.Forms;

public class A
{
    public virtual int method () {return 0;}
}
 
public class B
{
    public virtual int method () {return 1;}
}
 
class Mymethodinfo
{
    public static int Main()
    {
        Console.WriteLine ("\nReflection.MethodInfo");
        A MyA = new A();
        B MyB = new B();
 
        // Get the Type and MethodInfo.
        Type MyTypea = Type.GetType("A");
        MethodInfo Mymethodinfoa = MyTypea.GetMethod("method");
 
        Type MyTypeb = Type.GetType("B");
        MethodInfo Mymethodinfob = MyTypeb.GetMethod("method");
 
        // Get and display the Invoke method.
        Console.Write("\nFirst method - " + MyTypea.FullName +
            " returns " + Mymethodinfoa.Invoke(MyA, null));
        Console.Write("\nSecond method - " + MyTypeb.FullName +
            " returns " + Mymethodinfob.Invoke(MyB, null));
        return 0;
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.DLL>

using namespace System;
using namespace System::Reflection;

#define NULL 0

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

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

int main ()
{
    A *objA = new A();
    B *objB = new B();

    Type *typeA = objA->GetType();
    Type *typeB = objB->GetType();

    MethodInfo *methodA = typeA->GetMethod(L"method");
    MethodInfo *methodB = typeB->GetMethod(L"method");

    Console::WriteLine(S"\tUsing reflection to invoke A::method on objB returns {0}.",
             dynamic_cast<Object *>(methodA->Invoke(objB, NULL)));
    Console::WriteLine(S"\tUsing Invoke on objB after converting to type A returns {0}.",
             dynamic_cast<Object *>(methodA->Invoke(dynamic_cast<A *>(objB), NULL)));

    Console::WriteLine(S"\r\nThe same behavior is seen when the method ");
    Console::WriteLine(S"is invoked without reflection:\r\n");

    Console::WriteLine(S"\tDirectly invoking B::method on objB returns {0}.",
             __box(objB->method()) );
    Console::WriteLine(S"\tDirectly invoking B::method on objB after converting to type A returns {0}.",
             __box(dynamic_cast<A *>(objB)->method()));
};

[JScript] 
public class A
{
   public function method () : int {return 0;}
}

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

class Mymethodinfo
{
   public static function Main() : void
   {
      Console.WriteLine ("\nReflection.MethodInfo");
      var MyA : A = new A();
      var MyB : B = new B();

      //Get the Type and MethodInfo
      var MyTypea : Type = Type.GetType("A");
      var Mymethodinfoa : MethodInfo = MyTypea.GetMethod("method");

      var MyTypeb : Type = Type.GetType("B");
      var Mymethodinfob : MethodInfo = MyTypeb.GetMethod("method");

      //Get and display the Invoke method
      Console.Write("\nFirst method - " + MyTypea.FullName +
         " returns " + Mymethodinfoa.Invoke(MyA, null));
      Console.Write("\nSecond method - " + MyTypeb.FullName +
         " returns " + Mymethodinfob.Invoke(MyB, null));
    }
}
Mymethodinfo.Main();

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

MethodBase Class | MethodBase Members | System.Reflection Namespace | MethodBase.Invoke Overload List | BindingFlags | Missing | InvokeMember

Show: