Checking if Type instance is a nullable enum in C#

adrin picture adrin · Apr 27, 2010 · Viewed 17.1k times · Source

How do i check if a Type is a nullable enum in C# something like

Type t = GetMyType();
bool isEnum = t.IsEnum; //Type member
bool isNullableEnum = t.IsNullableEnum(); How to implement this extension method?

Answer

LukeH picture LukeH · Apr 27, 2010
public static bool IsNullableEnum(this Type t)
{
    Type u = Nullable.GetUnderlyingType(t);
    return (u != null) && u.IsEnum;
}