zoukankan      html  css  js  c++  java
  • C#枚举总结和其扩展用法(通过枚举描设置枚举值)

    C#中枚举是一个非常好用的类型,用会了之后确实方便了很多。

    项目中一个枚举类型:

    public enum Version_Type : byte
    {
        [Description("1997版")] 
        版本1997 = 0 ,
        [Description("2007版")] 
        版本2007 
    }

    枚举类型的默认类型是int型,可以改变其使用的类型,需要用(: <type>)来进行设置,上例中<type>为byte,也可以用其它类型(byte,sbyte,short,ushort,int,uint,long,ulong)

    访问控制符不可以为private,protected或者protected internal

    枚举有描述,枚举变量,枚举值。

    枚举描述可以没有,需要声明如上例需要加上引用 using System.ComponentModel;

    枚举变量不允许特殊字符(-,空格或者括号之类的(具体的以后补充)),允许使用下划线(_)不能以数字开头。因为枚举变量名称这些限制,所以有时候需要用描述来进行处理(如上例中枚举变量必须使用字符开头,所有需要对用户显示版本信息的时候“1997版”就不能直接当枚举变量,只能使用“版本1997”,有人说这样也能接受啊,但是当要有其它说明的时候比如有空格之类的特殊说明性文字就没法转换了。在C#利用反射动态根据传入属性创建对应控件文章中动态传入枚举,然后列出所有枚举项名称,因为枚举名称限制,所以后来改成了列出所有描述,然后选择描述名称,通过描述名称设置枚举值,即后文将介绍的枚举的扩展用法)

    枚举值自己定义,可以从零开始(默认也是0),下一个变量如果没有指定值则为上一个变量值+1,如上例中“版本2007”的值为1.

    枚举转换:

    1 Version_Type  myVersion = Version_Type.版本1997;
    2 byte version = (byte)myVersion;//获取值
    3 myVersion = (Version_Type )version;//获取枚举
    4 string versionName = myVersion.ToString();//获取枚举名称
    5 versionName = Convert.ToString(myVersion);//获取枚举名称和上一个一样
    6 string versionName1 = "版本2007";
    7 Version_Type  myVersion1 = (Version_Type )Enum.Parse(typeof(Version_Type),versionName1);//通过枚举名称获取枚举

    枚举扩展用法,通过枚举描述设置枚举值:

    这个方法关键还是对枚举描述和枚举变量进行记录。

    创建一个Dictionary全局对象:

    private Dictionary<int,Dictionary<string,string>> enumValue;

    int是用来存放属性索引,表示第i个属性。中间的Dictionary用来存放枚举描述和枚举变量名称,一一对应!

    Type t = spec.GetType();//获取类型
    for(int i=0;i<GetProperties().Getlength(0);i++)
    {
       string text = pi.GetValue(spec,null).ToString();
       //其它执行代码
       ....
       //如果是枚举类型
       if(pi.PropertyType.BaseType.Name == "Enum")
       {
        //创建ComboBoxCell
        ...
        //创建Dictionary 记录描述和属性的键值对
        Dictionary<string,string> itemEnum = new Dictionary<string,string>();
        //属性添加到  ComboBoxCell中
        foreach(var fi in pi.PropertyType.GetFields(BindingFlags.Static |  BindingFlags.Public))
            {
                  /********上篇文章用法**********
                 string text = fi.Name;
                 //text添加到控件中
                  ****************************/
                  object[] objs = fi.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute),false);
                  if(objs != null)
                      {
                             //获取描述
                              string strDescription = ((System.ComponentModel.DescriptionAttribute)objs[0]).Description;
                             //添加描述到控件中
                              ...
                             //集合中添加简直对
                             itemEnum.Add(strDescription,fi.Name);
                              continue;
                      }
                  //如果没有描述的情况
                  string strText = fi.Name;
                  //就用属性自己构成键值对
                  itemEnum.Add(strText,strText);
             }
              EnumValue.Add(i,itemEnum);
    } }

    当DataGridView中某一项改变的时候,在CellValidating事件处理函数中取出索引,取出要改变的值

    string strValue = e.FormattedValue.ToString();
    int index = e.RowIndex;
    string strValueSet = "";
    //获取描述对应的枚举属性
    if(EnumValue.ContainsKey(index))
    {
        strValueSet = EnumValue[index][strValue];
    }
    //后面修改值代码
  • 相关阅读:
    hdu 5726 GCD
    codeforces 982C Cut 'em all!
    codeforces 982B Bus of Characters
    codeforces 982A Row
    codeforces 983B XOR-pyramid
    codeforces 979D Kuro and GCD and XOR and SUM
    codeforces 983A Finite or not?
    codeforces 984B Minesweeper
    codeforces 979C Kuro and Walking Route
    codeforces 979B Treasure Hunt
  • 原文地址:https://www.cnblogs.com/Iamsorry/p/4776432.html
Copyright © 2011-2022 走看看