C 獲取列舉的key value格式

2021-09-27 09:26:45 字數 358 閱讀 4184

首先,在目前c#版本中並沒有乙個直接返回列舉的key-value形式的有效方法,可能在c#列舉的設計思想上並不至支援這樣使用,但列舉的格式很像鍵值對是有辦法滿足這一需求的。

隨便定義乙個列舉:

public enum en
返回列舉的key-value格式方法:

public static dictionarygetenumdic(type en) 

return enumdic;

}

呼叫:

dictionarydic = getenumdic1(typeof(en));

c 獲取列舉

在實際開發專案中,我們定義了乙個列舉,往往我們需要在下拉框或其它地方展示列舉。為了加深印象,也為了幫到有需要的人,我寫了乙個demo。第一步,我們定義乙個列舉 測試列舉 public enum enumstatus 第二步,為了方便檢視我們獲取的列舉,我們定義乙個列舉模型 定義列舉模型 public...

c 列舉的定義,列舉的用法,獲取列舉值

1.定義列舉型別 public enum test 2.獲取列舉值 public void enumsaction var g item.tostring 男 女 c typeof 和 gettype 區是什麼?1 typeof x 中的x,必須是具體的類名 型別 名稱等,不可以是變數名稱。2 ge...

c 列舉的定義,列舉的用法,獲取列舉值

1.定義列舉型別 public enum test 2.獲取列舉值 public void enumsaction var g item.tostring 男 女 對於typeof的解釋 c typeof 和 gettype 區是什麼?1 typeof x 中的x,必須是具體的類名 型別名稱等,不可...