title: Calling Task<T> methods generically
body: <p>Related to <a href="https://stackoverflow.com/questions/21805564/casting-tresult-in-tasktresult-to-system-object">Casting TResult in Task&lt;TResult&gt; to System.Object</a>. I am building a generic async command execution function for a service. </p>
  
  <p>The method to execute a generic command looks like this:</p>
  
  <pre><code>public async Task&lt;object&gt; ExecuteCommandAsync(string cmdName, CommandData data)
  </code></pre>
  
  <p>The code uses reflection to find a method on the class that has the given <code>cmdName</code>. Invoking this will return a <code>Type&lt;T&gt;</code> - we don't know the <code>T</code> in advance. However since <code>Task&lt;T&gt;</code> is not covariant, I cannot cast to <code>Task&lt;object&gt;</code>.</p>
  
  <p>At present my solution (as per 21805564) is to call the Result method and encapsulate in a <code>Task.Run</code>, as shown below:</p>
  
  <pre><code>// find the method
  MethodInfo cmd = GetMethod(cmdName);
  var task = (Task)cmd.Invoke(this, data);
  return await Task.Run&lt;object&gt;(() =&gt; { return task.GetType().GetProperty("Result").GetValue(task); });
  </code></pre>
  
  <p>My concern is that doing this negates the value of async: getting the result is now a blocking call so I might as well have used synchronous methods anyway.</p>