Author Topic: C# Reflection "Exception has been thrown by the target of an invocation"[Solved]  (Read 5303 times)


  • Administrator
  • Full Member
  • *****
  • Posts: 69
How to resolve the error in C# Reflection "Exception has been thrown by the target of an invocation"?

The most likely problem would be, you are invoking the method using C# reflection with wrong number of arguments. Refer to the following example, the function AddTwoNumbers would take only 3 arguments - 2 input and 1 output. If you give more or less number of arguments, you will get this error.

using System.Reflection;

namespace SFTCSClientConsole
    class Program
        static void Main(string[] args)

                Type mathType = Type.GetTypeFromProgID("SFTComServer.MathControl");

                object mathInstance = Activator.CreateInstance(mathType);

                object[] arguments = new object[5] { 10, 15, 25, 45, 55 };  // Would produce error - Exception has been thrown by the target of an invocation 

                object result = mathType.InvokeMember("AddTwoNumbers", BindingFlags.InvokeMethod,
                     null, mathInstance, arguments);

                if(arguments[2] != null)


Additional Information: For server code, refer to the attachment:


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1