c# 8 switch 表达式:没有找到 switch 表达式的最佳类型

Kam*_*aiz 14 c# .net-core asp.net-core c#-8.0 switch-expression

我在我的启动类(.net core 3.1)中添加了一个代码来返回基于参数的类型,我得到了编译时错误。

我在sharplab中创建了一个运行示例。如果 switch 表达式包含它运行良好的字符串或其他对象。

工作示例1:

var x = key switch
            {
                "myhandler1" => "something",
                "myhandler2" => "something else",
                _ => "default case"
            };
Run Code Online (Sandbox Code Playgroud)

工作示例2:

object obj =  s switch {
            "a" => new object(),
            "b" => new DateTime(),
            _ => throw new NotImplementedException()
        };
Run Code Online (Sandbox Code Playgroud)

错误示例:

interface IHandler { }
public class BaseHandler { }
public class MyHandler1: BaseHandler, IHandler { }
public class MyHandler2: BaseHandler, IHandler { }

class Program
{
    static void Main(string[] args)
    {

        var key = "myhandler1";

        var handler = key switch
        {
            "myhandler1" => new MyHandler1(),
            "myhandler2" => new MyHandler2(),
            _ => throw new NotImplementedException()
        };

        var x = key switch
        {
            "myhandler1" => "something",
            "myhandler2" => "something else",
            _ => "default case"
        };

        Console.WriteLine("Hello World!");
    }
}
Run Code Online (Sandbox Code Playgroud)

原始问题(需要修复):

serviceCollection.AddTransient<Func<string, IHandler>>(sp => key =>
            {
                return key switch
                {
                    Constants.Brand => sp.GetService<Handler1>(),
                    Constants.Series => sp.GetService<Handler2>(),
                    _ => throw new NotImplementedException()

                };
}
Run Code Online (Sandbox Code Playgroud)

找到这个链接:https : //github.com/dotnet/csharplang/issues/2728

感谢PavelMarc,以下是修复:

serviceCollection.AddTransient<Func<string, IHandler>>(sp => key =>
            {
                return key switch
                {
                    Constants.Brand => (sp.GetService<Handler1>() as IHandler),
                    Constants.Series => (sp.GetService<Handler2>() as IHandler),
                    _ => throw new NotImplementedException()

                };
}
Run Code Online (Sandbox Code Playgroud)

Pav*_*ski 18

您应该显式声明一种处理程序,而不是 var

IHandler handler = key switch //or BaseHandler handler = key switch
{
    "myhandler1" => new MyHandler1(),
    "myhandler2" => new MyHandler2(),
    _ => throw new NotImplementedException()
};
Run Code Online (Sandbox Code Playgroud)

在您的sharplab示例中,两个处理程序都实现了IHandler接口和继承BaseHandler类,编译器根本不知道要使用哪种类型,您应该明确地告诉他

interface IHandler { }
public class BaseHandler { }
public class MyHandler1 : BaseHandler, IHandler { }
public class MyHandler2 : BaseHandler, IHandler { }
Run Code Online (Sandbox Code Playgroud)

依赖注入示例也是如此,你应该显式声明一个类型(假设Handler1Handler2实现IHandler

return key switch
{
    Constants.Brand => sp.GetService<Handler1>(),
    Constants.Series => (IHandler) sp.GetService<Handler2>(),
    _ => throw new NotImplementedException()
};
Run Code Online (Sandbox Code Playgroud)

您只能为一个常量执行此操作,编译器足够聪明,可以为您完成其余工作