我想要一些像switch语句(但当然不同)

时间:2012-04-21 12:02:55

标签: c# c#-4.0

是否有像switch语句一样的C#结构允许控制进入下一级别?我有这样的事情:

    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...)但在我看来应该是这样。我知道它预计会有中断,但这不会给我提供我正在寻找的数据流。可以采取哪些措施来实现我想要的方式?

7 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:2)

与gotos等一起,你可以使用这个枚举和一点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
    }

答案 2 :(得分: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;
}

答案 3 :(得分: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;
        }

Microsoft(隐式)建议使用此方法:http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.71).aspx

答案 4 :(得分:1)

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

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();

答案 5 :(得分: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;

答案 6 :(得分: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;
}