Splitting CamelCase

前端 未结 15 2427
盖世英雄少女心
盖世英雄少女心 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:51

    public enum ControlSelectionType    
    {   
        NotApplicable = 1,   
        SingleSelectRadioButtons = 2,   
        SingleSelectDropDownList = 3,   
        MultiSelectCheckBox = 4,   
        MultiSelectListBox = 5   
    } 
    public class NameValue
    {
        public string Name { get; set; }
        public object Value { get; set; }
    }    
    public static List EnumToList(bool camelcase)
            {
                var array = (T[])(Enum.GetValues(typeof(T)).Cast()); 
                var array2 = Enum.GetNames(typeof(T)).ToArray(); 
                List lst = null;
                for (int i = 0; i < array.Length; i++)
                {
                    if (lst == null)
                        lst = new List();
                    string name = "";
                    if (camelcase)
                    {
                        name = array2[i].CamelCaseFriendly();
                    }
                    else
                        name = array2[i];
                    T value = array[i];
                    lst.Add(new NameValue { Name = name, Value = value });
                }
                return lst;
            }
            public static string CamelCaseFriendly(this string pascalCaseString)
            {
                Regex r = new Regex("(?<=[a-z])(?[A-Z])|(?<=.)(?[A-Z])(?=[a-z])");
                return r.Replace(pascalCaseString, " ${x}");
            }
    
    //In  your form 
    protected void Button1_Click1(object sender, EventArgs e)
            {
                DropDownList1.DataSource = GeneralClass.EnumToList(true); ;
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataValueField = "Value";
    
                DropDownList1.DataBind();
            }
    

提交回复
热议问题