2012-02-28 50 views
2

我想解決一個簡單的解析問題,我選擇使用枚舉來編碼選項列表。我怎樣才能一般地施放枚舉?

輸入數據是直接的ascii文本,分成具有唯一標頭和數據所在的非唯一標識符的塊。我能夠編寫相當一般的符號化方法,但不提供任何關於數據含義的上下文,並在返回後處理它。

用字符串做這件事沒有問題。我只是通過一個列表,我們走了。

我無法弄清楚枚舉枚舉的語法,我可以使用一些幫助。我也可能過於緊張,並且錯過了一個簡單的答案。

下面是與

private void parseToEnums(Enum returnEnum, string searchBlock, string startIDText, 
          string endIDText, string startText, string endText) 
{ 
    string ourSearchBlock = searchBlock; 
    int endIDidx = ourSearchBlock.IndexOf(endIDText); 

    while (ourSearchBlock.IndexOf(startText) != -1) 
    { 
     if (ourSearchBlock.Length == searchBlock.Length) 
     { 
      // first pass, trim off the region where the start text isn't valid 
      ourSearchBlock = ourSearchBlock.Remove(endIDidx, ourSearchBlock.Length - endIDidx); 
      // first pass, use the startIDtext to create a valid search zone 
      // BROKEN CODE HERE 
      // Neither GetType() nor typeof seem to do the right thing 
      // I have tried several varieties and have tried casting the LHS in the 
      // same sort of way 
      // pluckText returns a string that is guaranteed to match an enum name 
      returnEnum = (returnEnum.GetType()) System.Enum.Parse(typeof(returnEnum), pluckText(ourSearchBlock, startIDText, startText, endText), false); 
      ourSearchBlock = ourSearchBlock.Remove(0, ourSearchBlock.IndexOf(startIDText) + startIDText.Length); 
     } 
     else 
     { 
      // this would be similar to the above after it's working 
      // and is for the case where the string has multiple matches 
      // within the enum, ie "red white" 
      //returnList.Add(pluckText(ourSearchBlock, "", startText, endText)); 
     } 
     ourSearchBlock = ourSearchBlock.Remove(0, ourSearchBlock.IndexOf(startText) + startText.Length); 
    } 

    return; 
} 

例子中,我有一個很難的代碼我在做什麼

private enum Colors { red, white, green }; 
private enum Suits { spades, clubs, hearts, diamonds }; 

// ... open files, read data, etc 
// so I pass in the enum that I want my result in and some text identifiers 

parseToEnum (Colors, searchBlock, "startColorBlock", "endColorBlock", "id="); 
parseToEnum (Suits, searchBlock, "startCardSuitsBlock", "endCardSuitsBlock", "<id="); 

// ... 

這樣的想法是使用相同的結構(因爲輸入是相同),但對輸出使用不同的枚舉。

我知道我需要添加一些try/catch包裝和一般錯誤檢測到這個代碼之前太多。

回答

9

我將忽略所有搜索,並專注於將string轉換爲enum

首先,我認爲你的方法應該返回的結果,不通過它作爲參數(你需要out)。

其次,要將枚舉的類型傳遞給方法,可以使用類型爲Type的參數,或者更好的方法是使該方法爲泛型,並將該類型作爲類型參數傳遞。

的方法看起來是這樣的:

T ParseEnum<T>(string s) 
{ 
    return (T)Enum.Parse(typeof(T), s, false); 
} 

然後,您可以這樣調用它:

Colors color = ParseEnum<Colors>(someString); 

在你的代碼的錯誤是:

  • Enum是一種常見的基本類型全部爲enum s,但不代表enum的類型。這意味着您不能使用例如Colors作爲方法的參數。
  • 您不能轉換爲僅在運行時已知的類型。換句話說,像(foo.GetType())bar這樣的代碼將無法工作。
  • 您不能使用typeof運算符來獲取變量的類型。您可以使用它來獲取某個特定類型的對象,例如Typetypeof(string)typeof(T)在類型參數爲T的泛型方法中。
  • 類型名稱(包括enum s)應該是單數。這是因爲例如類型Color的變量代表一個顏色。雖然這只是一個樣式問題,但它不會阻止你的代碼工作。但它會讓你的代碼更難理解。
+0

隨意忽略搜索:-)如果我沒有包括它,有人會說我沒有提供足夠的細節。我很喜歡這個,並且會給它一個旋轉。 – Stephen 2012-02-28 21:52:48

+0

我也喜歡我可以寫顏色和= Par ...;用這種方法。 – Stephen 2012-02-28 21:54:05

+0

你寫這個,但它可能不會按你想要的方式工作。如果你想有一個'enum',它可以同時代表多個值,你必須將這些值設置爲不重疊的二進制值(例如1,2,4,8 ......),並使用'|'來組合他們。 – svick 2012-02-28 22:04:05