title: Cast Task<T> to Task<object> in C# without having T
body: <p>I have static class full of extension methods where each of the methods is asynchronous and returns some value - like this:</p>
  
  <pre><code>public static class MyContextExtensions{
    public static async Task&lt;bool&gt; SomeFunction(this DbContext myContext){
      bool output = false;
      //...doing stuff with myContext
      return output;
    }
  
    public static async Task&lt;List&lt;string&gt;&gt; SomeOtherFunction(this DbContext myContext){
      List&lt;string&gt; output = new List&lt;string&gt;();
      //...doing stuff with myContext
      return output;
    }
  }
  </code></pre>
  
  <p>My goal is to be able to invoke any of these methods from a single method in another class and return their result as an object. It would look something like this:</p>
  
  <pre><code>public class MyHub: Hub{
    public async Task&lt;object&gt; InvokeContextExtension(string methodName){
      using(var context = new DbContext()){
        //This fails because of invalid cast
        return await (Task&lt;object&gt;)typeof(MyContextExtensions).GetMethod(methodName).Invoke(null, context);
      }
    }
  }
  </code></pre>
  
  <p>The problem is that the cast fails. My dilemma is that I cannot pass any type parameters to the "InvokeContextExtension" method because it is part of a SignalR hub and is invoked by javascript. And to a certain extent I don't care about the return type of the extension method because it is just going to get serialized to JSON and sent back to the javascript client.  However I do have to cast the value returned by Invoke as a Task in order to use the await operator. And I have to supply a generic parameter with that "Task" otherwise it will treat the return type as void. So it all comes down to how do I successfully cast Task with generic parameter T to a Task with a generic parameter of object where T represents the output of the extension method.</p>