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)

public Object Invoke (
	Object obj,
	Object[] parameters
)
public final Object Invoke (
	Object obj, 
	Object[] parameters
)
public final function Invoke (
	obj : Object, 
	parameters : Object[]
) : Object
Not applicable.

Parameters

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 a null reference (Nothing in Visual Basic) 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 a 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 a 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

An object containing the return value of the invoked method, or a null reference (Nothing in Visual Basic) in the case of a constructor.

Exception typeCondition

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.

-or-

A static constructor is invoked, and obj is neither a 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 caller does not have permission to execute the constructor.

InvalidOperationException

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

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

NoteNote:

Starting with the , this method can be used to access non-public members if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit 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 . For more information, see The .NET Framework 3.5 Architecture.

The following code 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;
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;
    }
}

import System.*;
import System.Reflection.*;
import System.Windows.Forms.*;

public class A
{
    public int Method()
    {
        return 0;
    } //Method
} //A

public class B
{
    public int Method()
    {
        return 1;
    } //Method
} //B

class MyMethodInfo
{
    public static void main(String[] args)
    {
        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.get_FullName() 
            + " returns " + myMethodInfoA.Invoke(myA, null)));
        Console.Write(("\nSecond method - " + myTypeB.get_FullName() 
            + " returns " + myMethodInfoB.Invoke(myB, null)));
    } //main
} //MyMethodInfo

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();

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft