3

是否有一个 C# 构造,例如switch允许控制下降到下一个级别的语句?我有这样的事情:

    public static IEnumerable<string> SeverityOrHigher(string severity)
    {
        var result = new List<string>();

        switch (severity.ToUpper())
        {
            case "ALL":
                result.Add("ALL");
            case "DEBUG":
                result.Add("DEBUG");
            case "INFO":
                result.Add("INFO");
            case "WARN":
                result.Add("WARN");
            case "ERROR":
                result.Add("ERROR");
            case "FATAL":
                result.Add("FATAL");
            case "OFF":
                result.Add("OFF");
            default:
                break;
        }

        return result;
    }

...这显然在 C# 中不起作用,( Control cannot fall through from one case label...) 但在我看来它应该如此。我知道它会在那里中断,但这不会给我正在寻找的数据流。可以做些什么来让这一切以我想要的方式发生?

4

7 回答 7

10

在你的情况下,你可以用一点 LINQ 来模拟“下降的情况”:

public static IEnumerable<string> SeverityOrHigher(string severity)
{
    var result = new List<string>() 
        { "ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "OFF" };
    return result.SkipWhile(l => l != severity.ToUpper()).ToArray();
}
于 2012-04-21T12:11:17.760 回答
2

除了 goto 等,您还可以使用枚举和一些 linq:

    public static IEnumerable<Severity> SeverityOrHigher(Severity severity)
    {
        var value = (int) severity;

        return Enum.GetValues(typeof (Severity))
            .Cast<int>()
            .Where(i => i >= value)
            .Select(i => (Severity) i);
    }

    public enum Severity
    {
        All = 0,
        Trace = 1,
        Debug = 2,
        Information = 3,
        Warning = 4,
        Error = 5,
        Fatal = 6
    }
于 2012-04-21T12:16:22.427 回答
1

这不是最佳解决方案,但您可以使用如下goto语句:

switch (severity.ToUpper())
{
  case "ALL":
    result.Add("ALL");
    goto case "DEBUG";
  case "DEBUG":
    result.Add("DEBUG");
    goto case "INFO";
  case "INFO":
    result.Add("INFO");
    goto case "WARN";
  case "WARN":
    result.Add("WARN");
    goto case "ERROR";
  case "ERROR":
    result.Add("ERROR");
    goto case "FATAL";
  case "FATAL":
    result.Add("FATAL");
    goto case "OFF";
  case "OFF":
    result.Add("OFF");
    break;
  default:
    break;
}
于 2012-04-21T12:12:44.887 回答
1

使用转到:

        switch (severity.ToUpper())
        {
            case "ALL":
                result.Add("ALL");
                goto case "DEBUG";
            case "DEBUG":
                result.Add("DEBUG");
                goto case "INFO";
            case "INFO":
                result.Add("INFO");
                goto case "WARN";
            case "WARN":
                result.Add("WARN");
                goto case "ERROR";
            case "ERROR":
                result.Add("ERROR");
                goto case "FATAL";
            case "FATAL":
                result.Add("FATAL");
                goto case "OFF";
            case "OFF":
                result.Add("OFF");
                break;
            default:
                break;
        }

微软(含蓄地)推荐这种用法: http: //msdn.microsoft.com/en-us/library/06tc147t (v=vs.71).aspx

于 2012-04-21T12:14:24.463 回答
1

@nemesv 的 Linq 答案是更好的解决方案,但如果你想用 switch 来做,你可以这样做并且会得到相同的结果。

public static IEnumerable<string> SeverityOrHigher(string severity)
{
  var lastFound = -1;

  var severityList = new List<string>() { "ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "OFF" };
  var results = new List<string>();

  foreach (var t in severityList)
  {

    if (lastFound > -1)
    {
      for (var index = lastFound + 1; index < severityList.Count; index++)
      {
        results.Add(severityList[index]);
      }
      return results;
    }

    switch (severity.ToUpper())
    {
      case "ALL":
        results.Add(severity);
        lastFound = 0;
        break;
      case "DEBUG":
        lastFound = 1;
        results.Add(severity);
        break;
      case "INFO":
        lastFound = 2;
        results.Add(severity);
        break;
      case "WARN":
        lastFound = 3;
        results.Add(severity);
        break;
      case "ERROR":
        lastFound = 4;
        results.Add(severity);
        break;
      case "FATAL":
        lastFound = 5;
        results.Add(severity);
        break;
      case "OFF":
        lastFound = 6;
        results.Add(severity);
        break;
    }
  }

  return results;
}

测试:

  var list = SeverityOrHigher("ALL");
  foreach (var severity in list)
  {
    Console.WriteLine(severity);
  }

  Console.ReadKey();
于 2012-04-21T12:48:05.843 回答
0

看起来不是很好,但可以为您完成这项工作:

string s = severity.ToUpper();
result.add("OFF");
if (s == "OFF")
  return result;
result.add("FATAL");
if (s == "FATAL")
  return result;
result.add("ERROR");
if (s == "ERROR")
  return result;
// ...
result.add("ALL");
return result;
于 2012-04-21T12:06:46.253 回答
0

我会选择创建另一个代表所有有效严重性的列表并检查输入严重性是否是其中之一:

public static IEnumerable<string> SeverityOrHigher(string severity)
{
    var result = new List<string>();
    var severities = new List<string> { "ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "OFF" };

    severity = severity.ToUpper();

    if (severities.Contain(severity))
            result.Add(severity);

    return result;
}
于 2012-04-21T12:13:25.993 回答