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