Splitting CamelCase

前端 未结 15 2505
盖世英雄少女心
盖世英雄少女心 2020-12-07 10:56

This is all asp.net c#.

I have an enum

public enum ControlSelectionType 
{
    NotApplicable = 1,
    SingleSelectRadioButtons = 2,
    SingleSelectD         


        
15条回答
  •  孤街浪徒
    2020-12-07 11:42

    Indeed a regex/replace is the way to go as described in the other answer, however this might also be of use to you if you wanted to go a different direction

        using System.ComponentModel;
        using System.Reflection;
    

    ...

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

    this will allow you define your Enums as

    public enum ControlSelectionType 
    {
        [Description("Not Applicable")]
        NotApplicable = 1,
        [Description("Single Select Radio Buttons")]
        SingleSelectRadioButtons = 2,
        [Description("Completely Different Display Text")]
        SingleSelectDropDownList = 3,
    }
    

    Taken from

    http://www.codeguru.com/forum/archive/index.php/t-412868.html

提交回复
热议问题