.Net Invoke async method and await

C#.NetAsync Await

C# Problem Overview


I have an ansyc method

public Task<Car> GetCar()
{

}

I can call this method async and await:

 Car car = await GetCar()

How can I invoke the method using MethodInfo.Invoke and await for the result asynchronously.

 MethodInfo method = obj.GetMethod("GetCar");
 method.Invoke( obj, null)

C# Solutions


Solution 1 - C#

You can invoke it normally and then await the returned task:

Task<Car> result = (Task<Car>)method.Invoke(obj, null);
await result;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionajpView Question on Stackoverflow
Solution 1 - C#Stephen ClearyView Answer on Stackoverflow