【问题标题】:Can I use a space between two words in enumeration? [duplicate]我可以在枚举中的两个单词之间使用空格吗? [复制]
【发布时间】:2013-04-01 22:59:53
【问题描述】:

我可以像下面这样使用枚举吗?

我可以像 TeamManager、TeamLeader、SeniorDeveloper 等那样做。 但我想在“团队经理”之类的词之间留一个空格

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

【问题讨论】:

  • 这就是 Underscore 来拯救的地方。
  • 如果您想知道为什么不能,请让自己更深入地了解编译器解析符号...
  • @yogi:这与 C# 中的命名准则相矛盾。不要那样做。
  • 你为什么要这个?不是最方便的智能感知。

标签: c# c#-4.0


【解决方案1】:

不,但您可以这样做:

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
}

【讨论】:

  • 虽然这可行,但在必须翻译字符串的情况下并不理想。从事翻译工作的人喜欢将所有字符串放在一个地方,而不是分散在代码中......
  • Enum ToString 的可能副本
【解决方案2】:

不能提供空格,因为它会产生编译错误。如果您希望它用于显示目的。然后你可以编写一个方法,通过枚举返回一个 UI 友好的字符串

类似:

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

或按照@Christian Hayter 的建议在枚举中使用属性

【讨论】:

【解决方案3】:

不,你不能,这不能编译。

您可以在 Senior_Developer 之间添加下划线,但您必须自问我是在写代码还是在写论文。不要误会我的意思,代码应该清晰易读,但不必看起来像一个句子。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    相关资源
    最近更新 更多