为什么我在 (Func<IInterface>)
之间会出现不明确的调用错误和 (Func<Task<IInterface>>)
对于下一个代码示例?以及如何在不替换方法组调用的情况下避免此错误?
public class Program
{
public static void Main(string[] args)
{
Method(GetObject);
}
public static IInterface GetObject() => null;
public static void Method(Func<IInterface> func) =>
Console.WriteLine("First");
public static void Method(Func<Task<IInterface>> funcAsync) =>
Console.WriteLine("Second");
}
public interface IInterface { }
最佳答案
这将解决问题,因为您的方法需要一个返回 IInterface 的函数
public class Program
{
public static void Main(string[] args)
{
Method(() => GetObject());
}
public static IInterface GetObject() => null;
public static void Method(Func<IInterface> func) =>
Console.WriteLine("First");
public static void Method(Func<Task<IInterface>> funcAsync) =>
Console.WriteLine("Second");
}
public interface IInterface { }
关于c# - (Func<IInterface>) 和 (Func<Task<IInterface>>) 之间的调用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45373165/