我正在编写一个简单的 C# 控制台应用程序,它使用异步任务和 Entity Framework (打算在 Linux (RHEL) 下使用 Mono 运行它,但这是另一个挑战)。请注意,我的目标是 .NET 4.0,所以我使用的是 .ContinueWith()
而不是 await
.
这,加上 Northwind 数据库的 EF DB 模型,就是整个应用程序:
using System;
using System.Linq;
using System.Threading.Tasks;
namespace MonoEF
{
class Program
{
private static Model.NorthwindEntities _db = new Model.NorthwindEntities();
static void Main(string[] args)
{
try
{
GetCustomerNamesAsync().ContinueWith(t => {
if (t.IsFaulted) Console.WriteLine(t.Exception.Flatten.ToString);
else if (t.IsCompleted) foreach (string result in t.Result) Console.WriteLine(result);
});
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static Task<string[]> GetCustomerNamesAsync()
{
return Task.Factory.StartNew(() => (from c in _db.Customers select c.ContactName).Distinct().ToArray());
}
}
}
问题是我在
.ContinueWith()
处收到以下错误:
Ambiguous Invocation:
System.Threading.Tasks.Task.ContinueWith(System.Action<System.Threading.Tasks.Task<string[]>>) (in class Task<string[]>)
System.Threading.Tasks.Task.ContinueWith(System.Action<System.Threading.Tasks.Task>) (in class Task)
match
对我来说,调用不应该是模棱两可的,编译器应该更喜欢通用任务而不是非通用任务,特别是因为它是
GetCustomerNamesAsync()
的输出.但是,作为 VB.NET 开发人员,我可能依赖于
Option Infer
在这种情况下。
我将如何明确地让编译器知道我希望它在 C# 中使用哪个调用?
请您参考如下方法:
尝试明确指定 lambda 参数类型,如下所示:
.ContinueWith((Task<string[]> t) => { ... })
您调用它的方式存在的问题是
Task<TResult>
和
Task
(它的基类)都有一个
ContinueWith
看起来几乎相同的方法:
Task<TResult>.ContinueWith(Action<Task<TResult>> action)
Task<TResult>.ContinueWith(Action<Task> action) //inherited from `Task`
不指定
action
的输入类型,编译器无法确定您想要哪种重载。明确提供
action
lambda 的输入参数类型消除了这种歧义。
如果编译器可以假设采用
Action<Task<TResult>> action
的版本当然会很好。 .也许其他人对如何获得这种行为有想法?
为后人...
在评论中,您会看到 MCattle 发现他遇到这个问题只是因为在他的 lambda 内部的方法调用中缺少括号相关的一些编译器奇怪。通常,您不需要明确指定
Task<TResult>
将 lambda 传递给
ContinueWith
时输入.