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
  • 732,580
  • 175
  • 1,330
  • 1,459
BanksySan
  • 27,362
  • 33
  • 117
  • 216
  • 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ç
  • 100,147
  • 13
  • 119
  • 184