I am trying to create Expressionone that will reference a specific generic overloaded method ( Enumerable.Averagein my first test case). Specific type bindings are not yet known until execution, so I need to use Reflectionto search and create the correct general method ( Expressioncreated from parsed text).
So, if at runtime I know that I want to find this particular overload:
public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
How to resolve this MethodInfowith reflection?
So far, I have the following select 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 &&
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 a specific overload that takes Func<TSource, int>where argType == typeof(int).
I'm at a standstill and any help is appreciated.