Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Invoke async method and await [duplicate]

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) 
like image 519
ajp Avatar asked Apr 22 '13 17:04

ajp


1 Answers

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

Task<Car> result = (Task<Car>)method.Invoke(obj, null); await result; 
like image 94
Stephen Cleary Avatar answered Sep 20 '22 14:09

Stephen Cleary