how to correct the parameter count mismatch

The error doesn't need any correction, it is correct. ;)

You are trying to call a method that takes two parameters with an array of parameters that only contains one item.

A parameter array that would work for that specific method would for example be:

new object[] { 0, 1.5 }

If you want your InvokeMethod method to work with methods that take different number of parameters with different types, you have to create different parameter arrays for each combination.


Your InvokeMethod implementation always calls t.GetMethod(methodName).Invoke with two arguments, the first being the target instance on which the method is called, and second being the array of method arguments, which contains only one string (f.ReadLine()).

Then you use InvokeMethod to call MyClass.Method5 which takes two arguments, an int and a double. This obviously can't work, as myClass.Method5("some string") is syntactically incorrect, and this is what effectively happens. You can't expect that a string is a valid argument list for all MyClass methods, can you?

That is the cause of the error, but only you can decide how to fix it, as we don't know the greater context. You have to provide the correct number of parameters depending on the actual method being called.

Possible path to solution:

  • what are the arguments I want to provide to Method5?
  • where do I get them from?
  • how do I move them from wherever they are to the array I give to Invoke?

This should get you started, but no one can tell you exactly as you have only described the error, but not the real problem you are trying to solve with your code.

Tags:

C#

Reflection