2

I have a class with two methods, overloaded with identical name and arguments, but one is generic:

public class Foo
{
    public string Bar(string s) { throw new NotImplementedException(); }
    public T Bar<T>(string s) { throw new NotImplementedException(); }
}

How can I get the MethodInfo for one of these methods?

E.g:

var fooType = typeof(Foo);
var methodInfo = fooType.GetMethod("Bar", new[] { typeof(string) }); // <-- [System.Reflection.AmbiguousMatchException: Ambiguous match found.]

.NET Fiddle here

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
BanksySan
  • 25,929
  • 29
  • 105
  • 202
  • 2
    take a look at this: http://stackoverflow.com/questions/1465715/how-to-i-find-specific-generic-overload-using-reflection – Ehsan Sajjad Jan 26 '15 at 17:47

1 Answers1

5

You can use LINQ to get generic or non generic one:

fooType.GetMethods().First(m => m.Name == "Bar" && m.IsGenericMethod)

To get the non generic overload just negate the result of m.IsGenericMethod.

Selman Genç
  • 97,365
  • 13
  • 115
  • 182