Splitting CamelCase

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

This is all asp.net c#.

I have an enum

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


        
15条回答
  •  -上瘾入骨i
    2020-12-07 11:31

    The solution from Eoin Campbell works good except if you have a Web Service.

    You would need to do the Following as the Description Attribute is not serializable.

    [DataContract]
    public enum ControlSelectionType
    {
        [EnumMember(Value = "Not Applicable")]
        NotApplicable = 1,
        [EnumMember(Value = "Single Select Radio Buttons")]
        SingleSelectRadioButtons = 2,
        [EnumMember(Value = "Completely Different Display Text")]
        SingleSelectDropDownList = 3,
    }
    
    
    public static string GetDescriptionFromEnumValue(Enum value)
    {
        EnumMemberAttribute attribute = value.GetType()
            .GetField(value.ToString())
            .GetCustomAttributes(typeof(EnumMemberAttribute), false)
            .SingleOrDefault() as EnumMemberAttribute;
        return attribute == null ? value.ToString() : attribute.Value;
    }
    

提交回复
热议问题