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

私が間違っている場合は修正してください。ただし、引数をキー、時間を値として辞書を作成し、.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 に答える
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 に答える