C# 不能从 Task<> 隐式转换类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12886559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot implicitly convert type from Task<>
提问by PaulR
I am trying to master async method syntax in .NET 4.5. I thought I had understood the examples exactly however no matter what the type of the async method is (ie Task<T>), I always get the same type of error error on the conversion back to T- which I understood was pretty much automatic. The following code produces the error:
我正在尝试掌握 .NET 4.5 中的异步方法语法。我以为我已经完全理解了这些示例,但是无论异步方法的类型是什么(即Task<T>),我总是在转换回时遇到相同类型的错误错误T- 我理解这几乎是自动的。以下代码产生错误:
Cannot implicitly convert type '
System.Threading.Tasks.Task<System.Collections.Generic.List<int>>' to 'System.Collections.Generic.List<int>'
无法将类型“
System.Threading.Tasks.Task<System.Collections.Generic.List<int>>”隐式转换为“System.Collections.Generic.List<int>”
public List<int> TestGetMethod()
{
return GetIdList(); // compiler error on this line
}
async Task<List<int>> GetIdList()
{
using (HttpClient proxy = new HttpClient())
{
string response = await proxy.GetStringAsync("www.test.com");
List<int> idList = JsonConvert.DeserializeObject<List<int>>();
return idList;
}
}
It fails if I explicitly cast the result as well. This:
如果我也显式地转换结果,它也会失败。这个:
public List<int> TestGetMethod()
{
return (List<int>)GetIdList(); // compiler error on this line
}
somewhat predictably results in this error:
有点可预见地导致此错误:
Cannot convert type '
System.Threading.Tasks.Task<System.Collections.Generic.List<int>>' to 'System.Collections.Generic.List<int>'
无法将类型“
System.Threading.Tasks.Task<System.Collections.Generic.List<int>>”转换为“System.Collections.Generic.List<int>”
Any help greatly appreciated.
非常感谢任何帮助。
回答by James Manning
Depending on what you're trying to do, you can either block with GetIdList().Result ( generally a bad idea, but it's hard to tell the context) or use a test framework that supports async test methods and have the test method do var results = await GetIdList();
根据您要执行的操作,您可以使用 GetIdList().Result 阻止(通常是个坏主意,但很难说出上下文)或使用支持异步测试方法并让测试方法执行的测试框架var 结果 = await GetIdList();
回答by Mayank
You need to make TestGetMethod asynctoo and attach await in front of GetIdList();will unwrap the task to List<int>, So if your helper function is returning Task make sure you have await as you are calling the function asynctoo.
您也需要创建 TestGetMethodasync并在前面附加 awaitGetIdList();将任务解包到List<int>,因此,如果您的辅助函数正在返回 Task ,请确保您在调用该函数时async也有 await 。
public Task<List<int>> TestGetMethod()
{
return GetIdList();
}
async Task<List<int>> GetIdList()
{
using (HttpClient proxy = new HttpClient())
{
string response = await proxy.GetStringAsync("www.test.com");
List<int> idList = JsonConvert.DeserializeObject<List<int>>();
return idList;
}
}
Another option
另外一个选项
public async void TestGetMethod(List<int> results)
{
results = await GetIdList(); // await will unwrap the List<int>
}
回答by user2388853
The main issue with your example that you can't implicitly convert Task<T>return types to the base Ttype. You need to use the Task.Result property. Note that Task.Result will block async code, and should be used carefully.
您的示例的主要问题是您不能将Task<T>返回类型隐式转换为基本T类型。您需要使用 Task.Result 属性。请注意 Task.Result 会阻塞异步代码,应谨慎使用。
Try this instead:
试试这个:
public List<int> TestGetMethod()
{
return GetIdList().Result;
}

