有没有办法检查类型是一种类型的枚举?

use*_*667 5 c# enumeration typechecking

有人给我一个类型t.

我想知道这种类型是否是枚举.

public bool IsEnumeration(Type t)
{
    // Mystery Code.
    throw new NotImplementedException();
}

public void IsEnumerationChecker()
{
    Assert.IsTrue(IsEnumeration(typeof(Color)));
    Assert.IsFalse(IsEnumeration(typeof(float)));
}
Run Code Online (Sandbox Code Playgroud)

nan*_*nan 10

您还可以使用以下属性IsEnum 进行检查Type:

Type t = typeof(DayOfWeek);
bool isEnum = t.IsEnum;
Run Code Online (Sandbox Code Playgroud)