title: TargetException in RealProxy Invoke Message for generic type call in a method group
body: <p>Let's say I have an generic interface  <code>IService&lt;T&gt;</code> and a class implementing it <code>Service : IService&lt;Bar&gt;</code>
  I create a proxy of that interface :</p>
  
  <pre><code>var proxy = new DynamicProxy&lt;IService&lt;Bar&gt;&gt;(new Service()).GetTransparentProxy() as IService&lt;Bar&gt;;
  </code></pre>
  
  <p>DynamicProxy is a simple implementation of RealProxy :</p>
  
  <pre><code>    public class DynamicProxy&lt;I&gt; : RealProxy
      {
          private I _decorated;
  
          public DynamicProxy(I decorated) : base(typeof(I))
          {
              this._decorated = decorated;
          }
  
          public override IMessage Invoke(IMessage msg)
          {
              IMethodCallMessage methodCall = (IMethodCallMessage)msg;
              MethodInfo methodInfo = methodCall.MethodBase as MethodInfo;
  
              return new ReturnMessage(
                  methodInfo.Invoke(this._decorated, methodCall.InArgs),
                  null,
                  0,
                  methodCall.LogicalCallContext,
                  methodCall);
          }
      }
  </code></pre>
  
  <p>When using my proxy directly it works fine : </p>
  
  <pre><code>IEnumerable&lt;Bar&gt; bars = new List&lt;Bar&gt;() { new Bar { id = 2 }, new Bar { id = 3 } };
  proxy.Foo(bars.First());
  </code></pre>
  
  <p>or even with a lambda, it's fine :</p>
  
  <pre><code> var data = bars.ToList().Select(bar =&gt; proxy.Foo(bar)).ToList();
  </code></pre>
  
  <p>But when used with a method group, it throws a target exception</p>
  
  <pre><code>var data = bars.ToList().Select(proxy.Foo).ToList();
  </code></pre>
  
  <p>The thrown exception : </p>
  
  <pre><code>{System.Reflection.TargetException: Object does not match target type.
     at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
     at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
  </code></pre>
  
  <p>It seems that the realproxy doesn't manage to get the correct type of the generic :</p>
  
  <p>The MethodBase of the IMethodCallMessage is <code>{Int32 Foo(System.__Canon)}</code> instead of <code>{Int32 Foo(Bar)}</code></p>
  
  <p>Is it a limitation of the method group? or a bug in the RealProxy implementation? </p>
  
  <p>You can see it here:
  <a href="https://dotnetfiddle.net/w2VlVN" rel="nofollow noreferrer">https://dotnetfiddle.net/w2VlVN</a></p>
  
  <p>No luck in the <a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/0cf6e64d-d2a9-421e-8b4b-25a222adce7c/targetexception-with-realproxy-when-invoked-by-a-method-group-on-a-generic-interface-but-works-with?forum=netfxbcl" rel="nofollow noreferrer">MSDN forum</a>, How can I open a bug?</p>