Reputation: 6710
Somebody gives me a type t.
I'd like to know if that type is an enumeration or not.
public bool IsEnumeration(Type t)
{
// Mystery Code.
throw new NotImplementedException();
}
public void IsEnumerationChecker()
{
Assert.IsTrue(IsEnumeration(typeof(Color)));
Assert.IsFalse(IsEnumeration(typeof(float)));
}
Upvotes: 5
Views: 706
Reputation: 1503869
There are various ways you can achieve this:
return typeof(Enum).IsAssignableFrom(t) && t != typeof(Enum);
or
return typeof(Enum).IsAssignableFrom(t) && t.IsValueType;
or (now that I've seen it exists while checking IsValueType
)
return t.IsEnum;
Obviously the latter is the best approach, but the first two will give you hints about how to handle similar situations.
Upvotes: 3