常规迭代器块(即"yield return")是否与"async"和"await"不兼容?
这样可以很好地了解我正在尝试做的事情:
async Task<IEnumerable<Foo>> Method(String [] Strs)
{
// I want to compose the single result to the final result, so I use the SelectMany
var finalResult = UrlStrings.SelectMany(link => //i have an Urlstring Collection
await UrlString.DownLoadHtmlAsync() //download single result; DownLoadHtmlAsync method will Download the url's html code
);
return finalResult;
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到编译器错误,引用"无法从资源加载消息字符串".
这是另一种尝试:
async Task<IEnumerable<Foo>> Method(String [] Strs)
{
foreach(var str in strs)
{
yield return await DoSomethingAsync( str)
}
}
Run Code Online (Sandbox Code Playgroud)
但同样,编译器返回错误:"无法从资源加载消息字符串".
这是我项目中真正的编程代码
当我有一个List Task时,这非常有用,该任务可以从URL下载HTML,我使用语法"yield return await task",结果是我想要的 …