I am attempting to create an Expression
that will invoke a specific generic overloaded method (Enumerable.Average
in my first test case). The specific type bindings are not known until runtime however so I need to use Reflection
to find and create the correct generic method (the Expression
is being created from parsed text).
So if I know at runtime that I want to find this specific overload:
public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
How do I resolve that particular MethodInfo
using reflection?
So far I have the following selection statement:
MethodInfo GetMethod(Type argType, Type returnType)
{
var methods = from method in typeof(Enumerable).GetMethods(BindingFlags.Public | BindingFlags.Static)
where method.Name == "Average" &&
method.ContainsGenericParameters &&
method.GetParameters().Length == 2 &&
// and some condition where method.GetParameters()[1] is a Func that returns type argType
method.ReturnType == returnType
select method;
Debug.Assert(methods.Count() == 1);
return methods.FirstOrDefault();
}
The above narrows it down to three overloads but I want to reflect and find the specific overload that takes a Func<TSource, int>
where argType == typeof(int)
.
I am stumped and any help is appreciated.