2013-04-10 64 views
0

可以像下面那樣使用enum嗎?我可以在枚舉中使用兩個單詞之間的空格嗎?

我可以像TeamManager,TeamLeader,SeniorDeveloper等 但我想給喜歡「車隊經理」

public enum SoftwareEmployee 
{ 
    Team Manager = 1, 
    Team Leader = 2, 
    Senior Developer = 3, 
    Junior = 4 
} 
+2

這就是下劃線來拯救的地方。 – yogi 2013-04-10 08:00:22

+0

如果你想知道爲什麼你不能,讓自己更深入一個編譯器解析符號... – 2013-04-10 08:01:13

+1

@ yogi:這違背了C#中的命名準則。不要這樣做。 – 2013-04-10 08:01:30

回答

4

這是不可能的,因爲它會產生一個編譯錯誤。如果你想要它用於顯示目的。然後,你可以寫的時候通過枚舉返回UI友好的字符串的方法

是這樣的:由@Christian艾泰的建議

+0

[Enum ToString]的可能重複(http://stackoverflow.com/questions/479410/enum-tostring) – Oliver 2013-04-10 08:58:24

6

沒有言語之間的空間,但你可以做到這一點,而不是:

public enum SoftwareEmployee { 
    [Description("Team Manager")] TeamManager = 1, 
    [Description("Team Leader")] TeamLeader = 2, 
    [Description("Senior Developer")] SeniorDeveloper = 3, 
    [Description("Junior")] Junior = 4 
} 

然後,您可以使用一個實用的方法來枚舉值轉換爲描述,例如:

/// <summary> 
    /// Given an enum value, if a <see cref="DescriptionAttribute"/> attribute has been defined on it, then return that. 
    /// Otherwise return the enum name. 
    /// </summary> 
    /// <typeparam name="T">Enum type to look in</typeparam> 
    /// <param name="value">Enum value</param> 
    /// <returns>Description or name</returns> 
    public static string ToDescription<T>(this T value) where T : struct { 
     if(!typeof(T).IsEnum) { 
      throw new ArgumentException(Properties.Resources.TypeIsNotAnEnum, "T"); 
     } 
     var fieldName = Enum.GetName(typeof(T), value); 
     if(fieldName == null) { 
      return string.Empty; 
     } 
     var fieldInfo = typeof(T).GetField(fieldName, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static); 
     if(fieldInfo == null) { 
      return string.Empty; 
     } 
     var descriptionAttribute = (DescriptionAttribute) fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault(); 
     if(descriptionAttribute == null) { 
      return fieldInfo.Name; 
     } 
     return descriptionAttribute.Description; 
    } 

我比switch更喜歡手動翻譯,因爲如果所有內容都在一起,更容易維護枚舉定義。

要允許描述文本的本地化,請使用從資源中獲取其值的不同描述屬性,例如, ResourceDescription。只要它從Description繼承,那麼它會正常工作。例如:

public enum SoftwareEmployee { 
    [ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.TeamManager)] TeamManager = 1, 
    [ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.TeamLeader)] TeamLeader = 2, 
    [ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.SeniorDeveloper)] SeniorDeveloper = 3, 
    [ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.Junior)] Junior = 4 
} 
+0

雖然這可行,但在必須翻譯字符串的情況下,這並不理想。翻譯工作的人喜歡把所有的字符串放在一個地方,而不是散佈在代碼的周圍...... – 2013-04-10 08:11:43

+1

查看更新的答案。 – 2013-04-10 08:18:37

+0

[Enum ToString]的可能重複(http://stackoverflow.com/questions/479410/enum-tostring) – Oliver 2013-04-10 08:59:01

1

不,你不能

public string GetUIFriendlyString(SoftwareEmployee employee) 
{ 
    switch (employee): 
    { 
     case TeamLeader: return "Team Leader"; 
     // same for the rest 
    } 
} 

或使用屬性的枚舉,這無法編譯。

雖然你必須問自己我是在寫代碼還是在寫一篇文章,你可以在Senior_Developer這個詞之間加下劃線。不要誤解我的代碼應該是清晰的,但它不必看起來像一個句子。

我可以想到的唯一原因是您可能想要這樣做,以便您可以將其輸出到UI。諸如枚舉或異常之類的代碼不應該輸出到用戶界面中,開始時可能很有用,但應該使用某種映射將枚舉轉換爲純文本。如果您需要處理多個當地人,這特別有用。

相關問題