我可以在枚举中使用两个单词之间的空格吗?

我可以在枚举中使用两个单词之间的空格吗?

问题描述:

可以像下面那样使用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

这是不可能的,因为它会产生一个编译错误。如果你想要它用于显示目的。然后,你可以写的时候通过枚举返回UI友好的字符串的方法

是这样的:由@Christian艾泰的建议

+0

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

没有言语之间的空间,但你可以做到这一点,而不是:

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

不,你不能

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

或使用属性的枚举,这无法编译。

虽然你必须问自己我是在写代码还是在写一篇文章,你可以在Senior_Developer这个词之间加下划线。不要误解我的代码应该是清晰的,但它不必看起来像一个句子。

我可以想到的唯一原因是您可能想要这样做,以便您可以将其输出到UI。诸如枚举或异常之类的代码不应该输出到用户界面中,开始时可能很有用,但应该使用某种映射将枚举转换为纯文本。如果您需要处理多个当地人,这特别有用。