反射獲取列舉特性的小例子

2022-03-12 09:48:44 字數 1246 閱讀 9450

**如下:

using

system;

using

system.collections.generic;

using

system.collections.specialized;

using

system.reflection;

using

system.componentmodel;

namespace

", nvc[key]));

}dictionary

dic = getenumdic(typeof

(timeofday));

foreach(string key in

dic.keys)

",dic[key]);

}console.writeline(

string.format(timeofday.moning.tostring() + ":"

, getenumdes(timeofday.moning)));

}//////

從列舉型別和它的特性讀出並返回乙個鍵值對

/// ///

type,該引數的格式為typeof(需要讀的列舉型別)

///鍵值對 public

static

namevaluecollection getnvcfromenumvalue(type enumtype)

else

nvc.add(strvalue,strtext);}}

return

nvc;

}//////

返回 dic

《列舉項,描述》

/// ///

///dic《列舉項,描述》

public

static dictionarygetenumdic(type enumtype)

}return

dic;

}//////

獲得某個列舉項的描述

/// ///

///public

static

string getenumdes(object

value)

else}}

public

enum

timeofday

;public

enum

timeofdays

;}

c 反射 簡單小例子

介面using system usingsystem.text namespacejiang.iform voidrun 以下是實現介面的類 using system using system.drawing using system.text using system.windows.forms ...

C 通過反射獲取列舉值列表

通過type.getfields bindingflags.static 獲取列舉值列表,然後通過fieldinfo.getvalue null 就能獲取到值,注意是傳入null,像獲取靜態值和常量那樣。返回的值可以直接強轉為int,因為列舉的基礎型別就是int。如果獲取所有的字段資訊,會發現裡面有...

C 中的特性 和 通過反射獲取屬性上的特性

1.了解類 attributeusage attributeusage 有三個 屬性 分別是 public bool allowmultiple 作用 是否能在乙個目標身上多次使用 public bool inherited 作用 特性是否能繼承到子類身上 public attributetarge...