.NET异步调用方法,并等待 [英] .Net Invoke async method and await

查看:548
本文介绍了.NET异步调用方法,并等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ansyc方法

 公共任务<车> GetCar()
{

}
 

我可以调用此方法异步​​和等待:

 车车=等待GetCar()
 

我如何可以调用使用MethodInfo.Invoke的方法和等待的结果是异步的。

  MethodInfo的方法= obj.GetMethod(GetCar);
 method.Invoke(OBJ,空)
 

解决方案

您可以正常,然后调用它等待返回的任务:

 任务<车>结果=(任务<车>)method.Invoke(OBJ,NULL);
等待结果;
 

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)

解决方案

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

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

这篇关于.NET异步调用方法,并等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆