C#枚舉總結和其擴展用法(經過枚舉描設置枚舉值)

C#中枚舉是一個很是好用的類型,用會了以後確實方便了不少。html

項目中一個枚舉類型:函數

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

枚舉類型的默認類型是int型,能夠改變其使用的類型,須要用(: <type>)來進行設置,上例中<type>爲byte,也能夠用其它類型(byte,sbyte,short,ushort,int,uint,long,ulong)ui

訪問控制符不能夠爲private,protected或者protected internalspa

枚舉有描述,枚舉變量,枚舉值。code

枚舉描述能夠沒有,須要聲明如上例須要加上引用 using System.ComponentModel;orm

枚舉變量不容許特殊字符(-,空格或者括號之類的(具體的之後補充)),容許使用下劃線(_)不能以數字開頭。由於枚舉變量名稱這些限制,因此有時候須要用描述來進行處理(如上例中枚舉變量必須使用字符開頭,全部須要對用戶顯示版本信息的時候「1997版」就不能直接當枚舉變量,只能使用「版本1997」,有人說這樣也能接受啊,可是當要有其它說明的時候好比有空格之類的特殊說明性文字就無法轉換了。在C#利用反射動態根據傳入屬性建立對應控件文章中動態傳入枚舉,而後列出全部枚舉項名稱,由於枚舉名稱限制,因此後來改爲了列出全部描述,而後選擇描述名稱,經過描述名稱設置枚舉值,即後文將介紹的枚舉的擴展用法)htm

枚舉值本身定義,能夠從零開始(默認也是0),下一個變量若是沒有指定值則爲上一個變量值+1,如上例中「版本2007」的值爲1.對象

枚舉轉換:blog

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];
}
//後面修改值代碼
相關文章
相關標籤/搜索