2

所以我有这个开关是为了设置等待时间

public int Option(string arg)
{
    switch (arg)
    {
        case "/t:Max":
                return 0;
        case "/t:Med":
                return 1000;
        case "/t:Min":
               return 2000;
        default: return 0;
    }
}

如何使用 /t:Min、/t:Med、/t.Max 的枚举来替换开关?

确实是这样的:

enum t
{
    /t:Min,
    /t:Med,
    /t:Max
};
4

5 回答 5

3

你应该使用Enum.Parse. TryParse另请参阅:http: //msdn.microsoft.com/en-us/library/system.enum.parse.aspx

但是您仍然必须从命令行参数中检索字符串(去除 '/t:')并将其解析到您的Enum.

于 2012-08-02T13:38:11.413 回答
3

你的枚举应该是这样的:

public enum WaitTime 
{
     Min, Max, Med
}

并将您的开关转换为:

switch ((WaitTime)Enum.Parse(typeof(WaitTime), arg.Replace("/:", string.Empty)))
{
    case WaitTime.Max:
            return 0;
    case WaitTime.Med:
            return 1000;
    case WaitTime.Min:
           return 2000;
    default:
           return 0;
}
于 2012-08-02T13:38:45.667 回答
1

试试这个:

public enum t
{
  [Description("/t:Min")]  // <<---- set the string equivalent of the enum HERE.
  min = 0,  // <<---- set the return value of the enum HERE.

  [Description("/t:Med")]
  med = 1000,

  [Description("/t:Max")]
  max = 2000
}

您还需要这个方便的小类:

public static class EnumHelper
{
    public static list_of_t = Enum.GetValues(typeof(t)).Cast<t>().ToList();

    // returns the string description of the enum.
    public static string GetEnumDescription(this Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fi.GetCustomAttributes(
            typeof(DescriptionAttribute),
            false);

        if (attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }
}

您可以使用这样的装饰枚举来获得所需的内容:

public int Option(string arg)
{
  // get the matching enum for the "/t:" switch here.
  var foundItem = 
      EnumHelper.list_of_t.SingleOrDefault(c => c.GetEnumDescription() == arg);

  if (foundItem != null)
  {
    return (int)foundItem;  // <<-- this will return 0, 1000, or 2000 in this example.
  }
  else
  {
    return 0;
  }
}

HTH...

于 2012-08-02T13:54:48.050 回答
0

代码:

enum YourEnum
{
    Min,
    Med,
    Max
};

void Main(string[] args)
{
    var arg0 = args[0]; // "/t:Min"
    var arg0arr = arg0.Split(':') // { "/t", "Min" }
    var arg0val = arg0arr[1]; // "Min"
    var e = Enum.Parse(typeof(YourEnum), arg0val);
}

呼叫应用程序:

app.exe /t:Min
于 2012-08-02T13:44:10.183 回答
0

如果我错了,请纠正我,但不是最简单的方法只是制作一个以 arg 作为键和时间作为值的字典,并使用 .TryGetValue() 如果失败只返回 0?

像这样: arguments.Add 可能发生在构造函数中,不需要在方法中。

    private static Dictionary<string, int> arguments = new Dictionary<string, int>(5);

    public int Option(string arg)
    {
        arguments.Add("Max", 0);
        arguments.Add("Med", 1000);
        arguments.Add("Min", 2000);

        int value;
        if (arguments.TryGetValue(arg.Replace("/:", string.Empty), out value))
            return value;
        //default
        return 0;
    }
于 2012-08-02T13:51:39.493 回答