TargetParameterCountException: Parameter count mismatch

When you are using reflection to call a method, you may run into this exception:

System.Reflection.TargetParameterCountException: Parameter count mismatch.

This exception is straightforward – you aren’t passing in the correct number of parameters to MethodInfo.Invoke().

This article shows three different cases where you might run into this exception when using reflection.

Using reflection to invoke a method

The simplest case is when you are invoking a method and simply not passing in all of the parameters. The method definition may have changed, or you simply left out one of the parameters. This mistake is easy to make, since the compiler doesn’t help you catch it.

Let’s say you have the following method that returns the sum of two integers:

public class Util { public int Add(int a, int b) { return a + b; } }
Code language: C# (cs)

The following code uses reflection to call this method:

Util util = new Util(); int a = 1; int b = 2; var method = util.GetType().GetMethod("Add"); var parameters = new object[] { a }; var sum = method.Invoke(util, parameters);
Code language: C# (cs)

The Util.Add() method has two parameters: a and b. This code is only passing in one of the parameters – a – hence why it’s throwing TargetParameterCountException.

The solution is simple, pass in all of the parameters. In this case, we simply need to pass in b.

Util util = new Util(); int a = 1; int b = 2; var method = util.GetType().GetMethod("Add"); var parameters = new object[] { a, b }; var sum = method.Invoke(util, parameters);
Code language: C# (cs)

Using reflection to invoke a method with default parameters

Default parameters don’t work the same with reflection. When you have a method with default parameters, and you try calling it with reflection, you must pass in all of the parameters, default or not.

The following code subtracts two integers and returns the difference. It has a default parameter – b.

public class Util { public int Subtract(int a, int b = 0) { return a - b; } }
Code language: C# (cs)

The following code uses reflection to call this method. It’s only passing in one of the parameters.

Util util = new Util(); int a = 1; int b = 2; var method = util.GetType().GetMethod("Subtract"); var parameters = new object[] { a }; var difference = method.Invoke(util, parameters);
Code language: C# (cs)

Util.Subtract() expects two parameters – a and b. Only one parameter is being passed in – a – hence why this is throwing TargetParameterCountException.

But wait, doesn’t this have a default parameter? Yes, but default parameters basically don’t work with reflection. You must pass in all parameters, even if it’s a default parameter.

The solution is simple, pass in both parameters:

Util util = new Util(); int a = 1; int b = 2; var method = util.GetType().GetMethod("Subtract"); var parameters = new object[] { a, b }; var difference = method.Invoke(util, parameters);
Code language: C# (cs)

Using reflection to invoke an extension method

Extension methods are just static methods. The method is defined in one static class, and called on another type. As you may have guessed, when you call the extension method with reflection, you have to do it differently than you normally would.

Let’s say you have the following extension method:

public static class Util { public static int Multiply(this int a, int b) { return a * b; } }
Code language: C# (cs)

Without reflection, you’d call it like this:

a.Multiply(b);
Code language: C# (cs)

With reflection, you’d call it like this:

int a = 6; int b = 3; var method = typeof(Util).GetMethod("Multiply", BindingFlags.Static | BindingFlags.Public); var parameters = new object[] { a, b }; var product = method.Invoke(null, parameters);
Code language: C# (cs)

First, when you use reflection to call a static method, the first parameter to Invoke() is null, because you’re not calling it on an instance.

Second, as you can see on the highlighted line, you must pass in all of the parameters (just like when you have default parameters). Otherwise it’ll throw TargetParameterCountException.

Leave a Comment