我正在上 WinRT IAsyncOperation、async、await 的速成课程。我正在尝试用 IAsyncOperation 包装任务,但未能想出一种简单的方法来进行强制转换。
public interface IInfo{}
public class InfoList : IInfo{}
public class Parser {
private async Task<InfoList> ParseList() {
var returnList = new InfoList();
HttpClient client = new HttpClient();
Task<string> htmlTask = client.GetStringAsync("http://stackoverflow.com");
string html = await htmlTask;
// parsing logic...
return returnList;
}
public IAsyncOperation<IInfo> Parse()
{
return AsyncInfo.Run(_ =>
Task.Run<IInfo>(async () =>
{
var returnList = await ParseList();
return returnList;
})
);
// I have tried following but causes InvalidCastException
//return (IAsyncOperation<IInfo>)ParseList().AsAsyncOperation<InfoList>();
}
}