在我的应用程序中,我有类似以下代码的内容:
C#编译器现在会报告我的对'Method'的调用是模棱两可的。我漏掉了什么吗?
class Program
{
static void Main(string[] args)
{
Method(uri => Task.FromResult(uri));
}
static void Method(Func<Uri, Uri> transformer)
{
throw new NotImplementedException();
}
static void Method(Func<Uri, Task<Uri>> transformer)
{
throw new NotImplementedException();
}
}
正如预期的那样,运行此代码将调用“Method”的第二个重载,即需要返回任务的函数委托。但是,如果我更改代码以避免在Main中使用匿名方法:
class Program
{
static void Main(string[] args)
{
Method(Method2);
}
static Task<Uri> Method2(Uri uri)
{
return Task.FromResult(uri);
}
static void Method(Func<Uri, Uri> transformer)
{
throw new NotImplementedException();
}
static void Method(Func<Uri, Task<Uri>> transformer)
{
throw new NotImplementedException();
}
}
C#编译器现在会报告我的对'Method'的调用是模棱两可的。我漏掉了什么吗?
Func<in T, out TResult>
中的TResult
是协变的。 - haim770Method((Func<Uri, Task<Uri>>) Method2);
。 - M.kazem AkhgaryFunc<Uri, Uri>
到Method2
显然没有隐式转换。 - haim770