C# MethodInfo Invoke(Object, Object[])


MethodInfo Invoke(Object, Object[]) Invokes the method or constructor represented by the current instance, using the specified parameters.


MethodInfo.Invoke(Object, Object[]) has the following syntax.

public Object Invoke(
  Object obj,
  Object[] parameters


MethodInfo.Invoke(Object, Object[]) has the following 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 null 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 null.
  • parameters - 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.


MethodInfo.Invoke(Object, Object[]) method returns An object containing the return value of the invoked method, or null in the case of a constructor. Caution Elements of the parameters array that represent parameters declared with the ref or out keyword may also be modified.


using System;/*  w w w  .  ja  v a 2s . co m*/
using System.Reflection;

public class MagicClass
    private int magicBaseValue;

    public MagicClass()
        magicBaseValue = 9;

    public int ItsMagic(int preMagic)
        return preMagic * magicBaseValue;

public class TestMethodInfo
    public static void Main()
        Type magicType = Type.GetType("MagicClass");
        ConstructorInfo magicConstructor = magicType.GetConstructor(Type.EmptyTypes);
        object magicClassObject = magicConstructor.Invoke(new object[]{});

        MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
        object magicValue = magicMethod.Invoke(magicClassObject, new object[]{100});

        Console.WriteLine("MethodInfo.Invoke() Example\n");
        Console.WriteLine("MagicClass.ItsMagic() returned: {0}", magicValue);

The code above generates the following result.

Home »
  C# Tutorial »
    System.Reflection »