Reflection
Overloaded Methods
.NET
Method Invocation
Programming Tips

How to use Reflection to Invoke an Overloaded Method in .NET

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Invoking an overloaded method through reflection in .NET is mostly a method-selection problem. You are not just looking up a method by name; you are telling the runtime which overload matches the parameter types you intend to pass.

Get the Right MethodInfo

When a type has several methods with the same name, calling GetMethod("Name") is often not enough. You need the overload that matches the parameter list.

csharp
1using System;
2using System.Reflection;
3
4public class Sample
5{
6    public void Display(string text) => Console.WriteLine($"string: {text}");
7    public void Display(int number) => Console.WriteLine($"int: {number}");
8}
9
10public class Program
11{
12    public static void Main()
13    {
14        Type type = typeof(Sample);
15        MethodInfo? method = type.GetMethod("Display", new[] { typeof(string) });
16
17        var instance = new Sample();
18        method?.Invoke(instance, new object[] { "hello" });
19    }
20}

The key step is the second argument to GetMethod: it tells reflection which overload you want.

Match the Parameter Types Exactly

Reflection overload resolution is much less forgiving than a normal source-code method call. If the runtime sees several possible overloads, you should be explicit.

csharp
MethodInfo? intMethod = type.GetMethod("Display", new[] { typeof(int) });
intMethod?.Invoke(instance, new object[] { 42 });

This works because the requested signature and the provided invocation argument line up cleanly.

If you have optional parameters, nullable types, or more complex overload sets, being explicit becomes even more important.

Use BindingFlags When Needed

If the target method is non-public, static, or inherited in a way that default lookup does not catch, add the appropriate BindingFlags.

csharp
1using System;
2using System.Reflection;
3
4public class Sample
5{
6    private static void Hidden(string message)
7    {
8        Console.WriteLine(message);
9    }
10}
11
12public class Program
13{
14    public static void Main()
15    {
16        Type type = typeof(Sample);
17        MethodInfo? method = type.GetMethod(
18            "Hidden",
19            BindingFlags.NonPublic | BindingFlags.Static,
20            binder: null,
21            types: new[] { typeof(string) },
22            modifiers: null
23        );
24
25        method?.Invoke(null, new object[] { "secret call" });
26    }
27}

Without the correct binding flags, the method lookup may return null even though the method exists.

Understand Argument Conversion Limits

Normal C# calls can apply compile-time conversions that reflection will not guess for you automatically. That means the runtime argument array should already contain values of the expected type or something the binder can clearly use.

For overloaded methods, that is another reason to avoid vague reflection code such as "pick a method by name and hope the binder finds the right one." In practical systems, explicit signature matching is the safer approach.

Cache MethodInfo if Reflection Is Repeated

Reflection is flexible but slower and more error-prone than direct calls. If the same overloaded method is invoked repeatedly, resolve the MethodInfo once and cache it.

csharp
1using System;
2using System.Reflection;
3
4public class Sample
5{
6    public int Add(int a, int b) => a + b;
7}
8
9public class Program
10{
11    public static void Main()
12    {
13        var instance = new Sample();
14        MethodInfo method = typeof(Sample).GetMethod("Add", new[] { typeof(int), typeof(int) })!;
15
16        for (int i = 0; i < 3; i++)
17        {
18            object? result = method.Invoke(instance, new object[] { i, i + 1 });
19            Console.WriteLine(result);
20        }
21    }
22}

This reduces repeated lookup cost and centralizes the overload resolution in one place.

Common Pitfalls

The most common mistake is calling GetMethod("Name") on a type that has several overloads and assuming the runtime will guess the intended one. Another is forgetting BindingFlags when the target is non-public or static.

Developers also often pass arguments whose runtime types do not line up with the selected overload, then blame reflection rather than the mismatch.

Finally, reflection exceptions are often wrapped, especially during invocation. When debugging, inspect the inner exception as well as the reflection call site.

Summary

  • For overloaded methods, select the method by both name and parameter types.
  • Use explicit BindingFlags when the method is non-public or static.
  • Make the runtime argument array match the overload signature clearly.
  • Cache MethodInfo when reflective invocation happens repeatedly.
  • Treat reflection as a precise tool, not as a substitute for vague method lookup.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.