根據列舉值取得描述和列舉定義字串

2021-06-08 01:11:29 字數 1467 閱讀 4237

最近改進了一下…… (2014-04-11)

1. 輔助類

using system;

using system.collections.generic;

using system.collections.specialized;

using system.componentmodel;

using system.linq;

using system.reflection;

using system.text;

namespace util

/// /// 根據列舉型別得到其所有的 值 與 列舉定義字串 的集合

///

///

///

public static namevaluecollection getenumstringfromenumvalue(type enumtype)

}return nvc;

}/// /// 擴充套件方法:根據列舉值得到屬性description中的描述, 如果沒有定義此屬性則返回空串

///

///

///

///

public static string toenumdescriptionstring(this int value, type enumtype)

/// /// 根據列舉型別得到其所有的 值 與 列舉定義description屬性 的集合

///

///

///

public static namevaluecollection getnvcfromenumvalue(type enumtype)

else

nvc.add(strvalue, strtext);}}

return nvc;

}}//end of class

}//end of namespace

2. 測試類

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.collections.specialized;

using system.componentmodel;

using system.reflection;

using util; //注意引用

static void main(string args)

, 列舉字串:, 描述:",

v, str, desc);

console.read();}}

}

3. 結果

獲取列舉文字值和描述資訊, 列舉轉字典

string name enum.getname value.gettype value 列舉文字值 aa c aa enum.parse typeof aa str 字串轉列舉 返回列舉項的描述資訊。要獲取描述資訊的列舉項。列舉想的描述資訊。public static string getdesc...

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,必須是具體的類名 型別名稱等,不可...