简体   繁体   English

当开关(枚举)未覆盖所有枚举值时是否可以发出警告AT COMPILE C#

[英]Is it possible to give out a warning when switch (enum) does not cover every enum value AT COMPILE C#

So I have 所以我有

public enum Type
{
    Cat,
    Dog,
    Horse,
}

private void SomeFunc(Type type)
{
    switch (type)
    {
        case Type.Cat:
            // ...
            break;
        case Type.Dog:
            // ...
            break;

        default:
            throw new System.Exception();
    }
}

Say that at first we only have Cat and Dog. 假设一开始我们只有猫和狗。 And we already wrote millions of funcs like SomeFunc that uses switch (type). 而且我们已经写了数百万个使用switch(type)的func,例如SomeFunc。

Now we introduced Horse so I'm going to apply Horse to every switch. 现在我们介绍了Horse,因此我将把Horse应用于每个开关。

But people in other working branches do not aware of this adding. 但是其他工作分支中的人并不知道这一增加。 And when their branches merged into master, there will be some func does not contain Horse in switches. 并且当它们的分支合并为master时,将在交换机中包含一些不包含Horse的func。

Well if we unit-test with every Type later we can get Exceptions but we cannot afford to that. 好吧,如果以后我们对每种类型进行单元测试,我们都可以获取异常,但我们负担不起。

If we can check if all cases are explicitly listed in every switch (enum) that would be perfect. 如果我们可以检查所有情况是否都在每个开关(枚举)中明确列出,那将是完美的。 Any ideas? 有任何想法吗? Is it even possible (without wrapping)? 甚至有可能(不包裹)?

No, it's not possible - at least not out of the box. 不,这是不可能的-至少不是开箱即用。 Switch statements in C# do not have to be exhaustive, and there's no way to require them to be exhaustive. C#中的switch语句不必是穷举的,也没有办法要求它们是穷举的。

(Switch expressions in C# 8 will give a warning if they're not exhaustive, but that probably doesn't help you right now.) (如果C#8中的switch 表达式不够详尽,则会发出警告,但现在可能对您没有帮助。)

You could write a Roslyn analyzer for this though. 可以为此编写一个Roslyn分析器 If you've written Roslyn analyzers before I'd expect it to be reasonably trivial. 如果您之前写过罗斯林分析仪,那我认为它是微不足道的。 If you haven't written a Roslyn analyzer before, it can take quite a while to get going, although it's interesting work. 如果您以前没有编写过Roslyn分析器,则可能需要花费相当长的时间,尽管这是一项有趣的工作。

Alternatively, use a 3rd party Roslyn analyzer. 或者,使用第三方Roslyn分析仪。 I've just found one that seems to work, but I haven't played with it in any depth. 我刚刚找到了一个似乎可行的工具,但没有深入研究它。 Here's an example: 这是一个例子:

Project file: 项目文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Roslyn.Analyzers" Version="1.0.3.4" PrivateAssets="All" />
  </ItemGroup>

</Project>

Code: 码:

using System;

enum Color
{
    Red, Green, Blue
}

sealed class Program
{
    static void Main(string[] args)
    {
        Color c = Color.Red;

        switch (c)
        {
            case Color.Red:
                Console.WriteLine("Red");
                break;
            case Color.Green:
                Console.WriteLine("Green");
                break;
//            case Color.Blue:
//                Console.WriteLine("Blue");
//                break;
            default:
                Console.WriteLine("Undefined color");
                break;
        }
    }
}

Result: 结果:

Program.cs(14,9): warning ENUM0001: Add missing switch cases. Program.cs(14,9):警告ENUM0001:添加缺少的开关盒。 A switch is considered incomplete if it is missing a possible value of the enum or the default case. 如果开关缺少枚举的可能值或默认情况,则认为该开关不完整。

I can't vouch for the quality, and you'll probably want to configure which of the analyzers in that package are enabled, but it proves that it's possible. 我不能保证质量,您可能要配置启用该软件包中的哪些分析仪,但事实证明这是可能的。 There may well be other analyzers out there that do the same thing - this was just the first search result I found. 可能还有其他分析仪在做同样的事情-这只是我发现的第一个搜索结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM