2012-08-02 3 views
3

그래서 나는전달 열거 명령 줄 인수

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에 대한 열거를 사용할 수있는 대기 시간을 설정하기 위해이 스위치가 스위치를 교체 하시겠습니까? 같은 사실

뭔가 :

enum t 
{ 
    /t:Min, 
    /t:Med, 
    /t:Max 
}; 
+2

Enum.TryParse()가 염두에 ... –

+1

@JamesMichaelHare : 문자열이 열거 형으로 바뀌지 만 스위치가 필요하지 않습니다. 숫자가 열거 형 상수에 인코딩되어 있지 않으면 해킹이라고 부릅니다 :-) –

+0

/t : 부분을 잃어야합니다. – hatchet

답변

3

귀하의 열거는 다음과 같이해야한다

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; 
} 
+0

@abatishchev 감사합니다;) –

3

당신은 TryParseEnum.Parse 사용해야합니다. 또한 참조 : http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx

하지만 여전히 명령 줄 인수 ('/ t :'를 제거하십시오)에서 문자열을 검색하고 Enum으로 구문 분석해야합니다.

public enum WaitTime 
{ 
    Min, Max, Med 
} 

을이에 스위치를 변환 :

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 
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; 
    } 
1

이 시도 :

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 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 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 ...