Get the result of Task <T> without knowing typeof T

I am working on a C # system, and the class has one function that returns an object System.Threading.Tasks.Taskand has the System.TypeReturnType property .

When ReturnType is null, I know that the method returns a Task object. But, unfortunately, there is no way to find out if the class that implements the interface returns, Task<ReturnType>or Task<object>, and I need to get the result of this method. I think the easiest way to do this is to convert Task<T>to Task<object>so that I can get the result and process it using the type value in ReturnType.

How to convert Task<T>to Task<object>without knowing type T?

public interface ITaskFactory
{

    ReadOnlyCollection<ParameterInfo> ParametersInfo { get; }

    Type ReturnType { get; }

    Task CreateTask (params object[] args);

}

I need to get the result returned Taskthat I got by callingCreateTask()

: http://dotnetfiddle.net/Bqwz0I

+4
2

Task<T> ( IEnumerable<T>, . , out ), , :

async Task<object> Do<T>(Task<T> task)
{
   // this won't compile: Cannot implicitly convert type 'Task<T>' to 'Task<object>'
   // return task;

    object result = await task;
    return result;
}
-1

- ContinueWith

    Task<T> task = null;
    Task<object> obj = task.ContinueWith(t => (object)t.Result);
-1

Source: https://habr.com/ru/post/1529511/


All Articles