I have static class full of extension methods where each of the methods is asynchronous and returns some value - like this:
public static class MyContextExtensions{
public static async Task<bool> SomeFunction(this DbContext myContext){
bool output = false;
//...doing stuff with myContext
return output;
}
public static async Task<List<string>> SomeOtherFunction(this DbContext myContext){
List<string> output = new List<string>();
//...doing stuff with myContext
return output;
}
}
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:
public class MyHub: Hub{
public async Task<object> InvokeContextExtension(string methodName){
using(var context = new DbContext()){
//This fails because of invalid cast
return await (Task<object>)typeof(MyContextExtensions).GetMethod(methodName).Invoke(null, context);
}
}
}
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.
You cannot cast
Task<T>
toTask<object>
, becauseTask<T>
is not covariant (it's not contravariant, either). The simplest solution would be to use some more reflection:This is slow and inefficient, but usable if this code is not executed often. As an aside, what is the use of having an asynchronous MakeMyClass1 method if you are going to block waiting for its result?
and Another possibility is to write an extension method to this purpose:
It is none-blocking solution and will preserve original state/exception of the Task.
I'd like to provide an implementation which is IMHO the best combination of the earlier answers:
Here you go:
I made a little extension method based on dasblinkenlight's answer:
Usage:
This way you can change
T
inTask<T>
to anything you want.For the best approach, without using reflection and dynamic ugly syntax, and without passing generic types. I would use two extension methods for achieving this goal.
Usage:
This my second approach, but not recommended:
Usage:
This my third approach, but not recommended: (similar to second one)
Usage:
The most efficient approach would be custom awaiter:
with the following usage:
The limitation of this approach is that the result is not a
Task<object>
but an awaitable object.In general, to convert a
Task<T>
toTask<object>
, I would simply go for the straightforward continuation mapping :(documentation link here)
However, your actual specific need is to invoke a
Task
by reflection and obtain its (unknown type) result .For this, you can refer to the complete dasblinkenlight's answer, which should fit your exact problem.