测试一个对象是否是 Enum

我想知道‘ theObject’是否是枚举(任何枚举类型)

 foreach (var item in Enum.GetValues(theObject.GetType())) {


//do something
}
43733 次浏览

If you have a Type, use the Type.IsEnum property, e.g.:

bool isEnum = theObject.GetType().IsEnum;

The question is the answer. :)

bool isEnum = theObject is Enum;

just use

if (theObject is Enum)
//is an enum

For generic type parameters, the parameter can be constrained rather than tested:

where T : Enum