title: How do I use reflection to call a generic method?
body: <p>What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime?</p>
  
  <p>Consider the following sample code - inside the <code>Example()</code> method, what's the most concise way to invoke <code>GenericMethod&lt;T&gt;()</code> using the <code>Type</code> stored in the <code>myType</code> variable?</p>
  
  <pre><code>public class Sample
  {
      public void Example(string typeName)
      {
          Type myType = FindType(typeName);
  
          // What goes here to call GenericMethod&lt;T&gt;()?
          GenericMethod&lt;myType&gt;(); // This doesn't work
  
          // What changes to call StaticMethod&lt;T&gt;()?
          Sample.StaticMethod&lt;myType&gt;(); // This also doesn't work
      }
  
      public void GenericMethod&lt;T&gt;()
      {
          // ...
      }
  
      public static void StaticMethod&lt;T&gt;()
      {
          //...
      }
  }
  </code></pre>