我有一个 async
函数。有多个级别的函数调用都返回一个 Task
。在此链中的某个点,我必须修改结果的值。我想在不中断 async
函数流程的情况下执行此操作。观察:
[HttpGet]
[Route("GetWeb")]
public async Task<IHttpActionResult> GetResult([FromUri] string url)
{
var result = await GetPageText(url);
return Ok(result);
}
private Task<string> GetPageText(string url)
{
Task<string> data = GetDataAsync(url);
//here I would like to manipulate the data variable
return data;
}
在函数 GetPageText
中,我如何在不中断异步流的情况下操作数据变量。这可能吗?
最佳答案
您无法更改任务的值(value),您需要创建一个新的。 async
的方法是等待原始结果并将该方法标记为 async
(只有当您需要原始任务的结果时才会出现这种情况):
private async Task<string> GetPageText(string url)
{
var result = await GetDataAsync(url);
return result + "bar";
}
关于c# - 如何更改任务中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27707935/