c# - (Func<IInterface>) 和 (Func<Task<IInterface>>) 之间的调用不明确

标签 c#

为什么我在 (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/

相关文章:

c# - ICLRRuntimeHost::ExecuteInDefaultAppDomain 无法执行应用程序

c# - 通过 Activator.CreateInstance 检查对象是否为接口(interface)

c# - 我如何将 RSAParameters 从 .net 转换为 .pem 文件,以便我可以在 php 中使用它

c# - 如何获取创建对象 'cells' 的范围?

c# - 将 Swift 转换为 C# 以在 Xamarin 中使用

c# - 将 odata $expand asp.net core 与 AutoMapper 一起使用时不返回扩展集合

c# - 如何使用 .NET API 列出 Azure Batch 帐户中的所有任务?

c# - 如果项目是等价的,则将项目从一个类映射到另一个类

c# - "Correct"与本地 Windows 服务通信的方式

c# - 如何在 Windows IOT 核心中的 Raspberry pi 上获得更好的 GPIO 引脚性能