【问题标题】:Replacing enum values with something meaningful [duplicate]用有意义的东西替换枚举值[重复]
【发布时间】:2013-07-25 18:08:44
【问题描述】:

我有一个枚举,我在我的应用程序中使用如下:

  public enum TaskStatus{
       WaitinForApproval,
       SubmittedForReview,
       .....
  }

当我在页面上显示它时:

SubmittedForReview

我想显示为

Submitted For Review

如何做到不费事?

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

创建一些 HTML Helper 并随时调用它

public static string ShowMyEnumTitle(this HtmlHelper helper, myEnum enumTitle)
        {
            string enumText = "";
            string result = "";
            enumText = String.Format("{0}", Enum.GetName(typeof(myEnum), enumTitle));
            for (int i = 0; i < enumText.Length; i++)
            {
                if ((int)enumText[i] >= 65 && (int)enumText[i] <= 90 && i != 0)
                {
                    result += " " + enumText[i];
                }
                else
                {
                    result += enumText[i];
                }
            }
            return result;
        }

此帮助程序根据大写字符分隔文本,因此您的“SubmittedForReview”文本将转换为“提交以供审核”,然后您可以在视图中调用此 html 帮助程序:

@Html.ShowMyEnumTitle(Model.myEnumMember)

【讨论】:

    【解决方案2】:

    这不是真正的方式,但应该可以。

    您可以创建一个Dictionary&lt;TaskStatus, string&gt;,并使用您想要显示的内容填充值。

    然后,在显示值之前,只需使用字典查找要显示的值。

    Dictionary<TaskStatus, string> myDictionary = new Dictionary<TaskStatus, string>();
    myDictionary.Add(TaskStatus.WaitinForApproval, "Waiting For Approval");
    
    string displayValue = myDictionary[TaskStatus.WaitinForApproval]; //Waiting For Approval
    

    如果你改变你的枚举,你将不得不更新它。

    【讨论】:

    • 我也在考虑扩展方法。我只是认为有一个简单的方法。
    【解决方案3】:

    您可以使用数据注释。

    using System.ComponentModel.DataAnnotations;
    
    public enum TaskStatus{
       [Display(Name = "Waiting for approval")]
       WaitinForApproval,
       [Display(Name = "Submitted for review")]
       SubmittedForReview,
       .....
      }
    

    这在程序中您想使用友好名称而不是成员的实际名称的任何地方都很有用。在创建 using 语句之前,请确保您在项目中引用了 System.ComponentModel.DataAnnotations。

    编辑

    关于其他人的答案,如果您使用 Description 属性,您需要使用反射来访问它。使用 display 属性会在 UI 中自动显示您想要的字符串。

    【讨论】:

    • 你试过这个吗?你知道它不起作用吗?
    • @DarthVader 有什么不好的?我没有在 VS 中运行它,但我知道它在概念上有效,我一直在使用它。
    • @DarthVader 我的错。我没有意识到你使用的是 asp。
    • 是的,你应该阅读这个问题
    【解决方案4】:

    您可以使用属性为每个枚举项分配一个字符串。
    例如:

    public enum TaskStatus{
       [Description("Waiting for Approval!!")]
       WaitinForApproval,
       [Description("Submmited For Review.")]
       SubmittedForReview,
       [Description("... w/e")]
       .....
    }
    

    然后使用反射访问它:

    public static string GetEnumDescription(Enum value) {
        FieldInfo fi = value.GetType().GetField(value.ToString());
    
        DescriptionAttribute[] attributes = 
            (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
    
        if (attributes != null && attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }
    


    用法:

    GetEnumDescription(TaskStatus.SubmittedForReview);
    

    来源:http://blog.spontaneouspublicity.com/associating-strings-with-enums-in-c

    【讨论】:

    • 前期工作更多,但从长远来看会更好。
    【解决方案5】:

    如果您总是希望大写字母表示分词,可以使用简单的正则表达式替换来添加空格。

        private static string EnumToString(TaskStatus status)
        {
            return Regex.Replace(status.ToString("F"), "(?<=\\w)([A-Z])", " $1");
        }
    

    此表达式匹配前面有任何字母的任何大写字母(向后看,因此您不会在开头添加空格)。如果您愿意,也可以轻松地将其作为扩展程序。

     Console.WriteLine( EnumToString( TaskStatus.SubmittedForReview ) ); // "Submitted For Review"
    

    我相信这种方法优于反射和属性,因为当/如果您添加更多枚举值时,它也不需要任何额外的工作。缺点是输出值与枚举值直接相关,不能更复杂或包含标点符号。

    【讨论】:

    • 这适用于大写首字母缩写词吗?
    猜你喜欢
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    相关资源
    最近更新 更多