title: Surprise: MethodInfor Invoke faster than direct call?
body: <p>I need to invoke a generic method by <code>MethodInfo.Invoke</code>, because I only have generic type at runtime. I heard about MethodInfo.Invoke performance issue, that it's a lot slower than call method directly. So, to test about performance, I created benchmark program:</p>
  
  <pre><code>using BenchmarkDotNet.Attributes;
  using BenchmarkDotNet.Running;
  using System;
  using System.Collections.Concurrent;
  using System.Collections.Generic;
  using System.Linq;
  using System.Reflection;
  using System.Threading.Tasks;
  
  namespace Benmarking
  {
      class Program
      {
          static void Main(string[] args)
          {
              var summary = BenchmarkRunner.Run&lt;BenchmarkTest&gt;();
          }
      }
  
      public class BenchmarkTest
      {
          private const int _count = 1000000;
          private static readonly ConcurrentDictionary&lt;Type, Type&gt; _typesResult = new ConcurrentDictionary&lt;Type, Type&gt;();
          private static readonly ConcurrentDictionary&lt;Type, MethodInfo&gt; _methodsInfo = new ConcurrentDictionary&lt;Type, MethodInfo&gt;();
  
          [Benchmark]
          public async Task&lt;object&gt; InvokeCastGenericCall()
          {
              return await ((dynamic)new Test()).GetList(new Respone&lt;object&gt;(), _count).ConfigureAwait(false);
          }
  
          [Benchmark]
          public async Task&lt;object&gt; DirectCall()
          {
              return await new Test().GetList(new Respone&lt;object&gt;(), _count).ConfigureAwait(false);
          }
  
          [Benchmark]
          public async Task&lt;object&gt; InvokeMethodCall()
          {
              var typeResult = typeof(object);
  
              var methodInfo = typeof(Test).GetMethod(nameof(Test.GetList)).MakeGenericMethod(typeResult);
  
              Task task = (Task)methodInfo.Invoke(new Test(), new object[] { new Respone&lt;object&gt;(), _count });
  
              await task.ConfigureAwait(false);
  
              return ((dynamic)task).Result;
          }
  
          [Benchmark]
          public async Task&lt;object&gt; InvokeMethodCall_Cache()
          {
              var typeResult = _typesResult.GetOrAdd(typeof(object), type =&gt; typeof(object));
  
              var methodInfo = _methodsInfo.GetOrAdd(typeResult, type =&gt; typeof(Test).GetMethod(nameof(Test.GetList)).MakeGenericMethod(type));
  
              Task task = (Task)methodInfo.Invoke(new Test(), new object[] { new Respone&lt;object&gt;(), _count });
  
              await task.ConfigureAwait(false);
  
              return ((dynamic)task).Result;
          }
      }
  
      public class Test
      {
          public async Task&lt;List&lt;T&gt;&gt; GetList&lt;T&gt;(Respone&lt;T&gt; input, int count) where T : class, new()
          {
              IEnumerable&lt;T&gt; Get()
              {
                  for (var i = 0; i &lt; count; i++)
                      yield return new T();
              }
  
              return await Task.FromResult(Get().ToList());
          }
      }
  
      public class Respone&lt;T&gt; where T : new()
      {
  
      }
  }
  </code></pre>
  
  <p>The results were strange, MethodInfo.Invoke even faster than call method directly. And the fastest is cast object to dynamic and call dynamic method.
  The result is present in below image:
  <a href="https://i.stack.imgur.com/w0eJ2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w0eJ2.png" alt="enter image description here"></a></p>
  
  <p>I was worried about where I made the wrong benchmark? Can anyone explain it to me? Thank you very much!</p>