referencing desired overloaded generic method
I can't find a way of using GetMethod that would do what you want. But you can get all the methods and go through the list until you find the method that you want.
Remember you need to call MakeGenericMethod before you can actually use it.
var allMethods = typeof (Example).GetMethods(BindingFlags.Public | BindingFlags.Static);
MethodInfo foundMi = allMethods.FirstOrDefault(
mi => mi.Name == "Foo" && mi.GetGenericArguments().Count() == 2);
if (foundMi != null)
{
MethodInfo closedMi = foundMi.MakeGenericMethod(new Type[] {typeof (int), typeof (string)});
Example example= new Example();
closedMi.Invoke(example, new object[] { 5 });
}
Here are the answers to your questions along with an example:
Yes, although there are two things really to be aware of here with generic methods, type inference and overload method resolution. Type inference occurs at compile time before the compiler tries to resolve overloaded method signatures. The compiler applies type inference logic to all generic methods that share the same name. In the overload resolution step, the compiler includes only those generic methods on which type inference succeeded. More here...
Please see the full example Console Application program code below that shows how several variants of the Foo method can be specified in creation of a MethodInfo object and then invoked using an Extension method:
Program.cs
class Program
{
static void Main(string[] args)
{
MethodInfo foo1 = typeof(Example).GetGenericMethod("Foo",
new[] { typeof(string) },
new[] { typeof(int) },
typeof(void));
MethodInfo foo2 = typeof(Example).GetGenericMethod("Foo",
new[] { typeof(string), typeof(int) },
new[] { typeof(int) },
typeof(void));
MethodInfo foo3 = typeof(Example).GetGenericMethod("Foo",
new[] { typeof(string) },
new[] { typeof(string) },
typeof(void));
MethodInfo foo4 = typeof(Example).GetGenericMethod("Foo",
new[] { typeof(string), typeof(int) },
new[] { typeof(int), typeof(string) },
typeof(string));
Console.WriteLine(foo1.Invoke(null, new object[] { 1 }));
Console.WriteLine(foo2.Invoke(null, new object[] { 1 }));
Console.WriteLine(foo3.Invoke(null, new object[] { "s" }));
Console.WriteLine(foo4.Invoke(null, new object[] { 1, "s" }));
}
}
Example.cs:
public class Example
{
public static void Foo<T>(int ID) { }
public static void Foo<T, U>(int ID) { }
public static void Foo<T>(string ID) { }
public static string Foo<T, U>(int intID, string ID) { return ID; }
}
Extensions.cs:
public static class Extensions
{
public static MethodInfo GetGenericMethod(this Type t, string name, Type[] genericArgTypes, Type[] argTypes, Type returnType)
{
MethodInfo foo1 = (from m in t.GetMethods(BindingFlags.Public | BindingFlags.Static)
where m.Name == name &&
m.GetGenericArguments().Length == genericArgTypes.Length &&
m.GetParameters().Select(pi => pi.ParameterType).SequenceEqual(argTypes) &&
m.ReturnType == returnType
select m).Single().MakeGenericMethod(genericArgTypes);
return foo1;
}
}