title: Create and invoke generic async method
body: <p>I have a situation where I need to determine the type of an object dynamically (compile time is fine) within a declaration for a generic.</p>
  
  <p>I have a method like this:</p>
  
  <pre><code>private async Task&lt;T&gt; Post&lt;T&gt;(string path, Request data)
  {
      var authPath = GetAuthenticatedPath(path);
      var response = await _client.PostAsJsonAsync&lt;Request&gt;(authPath, data);
      return response;
  }
  </code></pre>
  
  <p>The problem is that I really need it to operate more like this:</p>
  
  <pre><code>private async Task&lt;T&gt; Post&lt;T&gt;(string path, Request data)
  {
      var authPath = GetAuthenticatedPath(path);
      var response = await _client.PostAsJsonAsync&lt;data.GetType()&gt;(authPath, data);
      return response;
  }
  </code></pre>
  
  <p>Because I need it to format the data variable as it's ActualRequestType rather than the Request type when it does the conversion to JSON.  The problem is you can't do data.GetType() within the type declaration.</p>