¿Cómo verifico si un tipo es una enumeración anulable en C # algo así como
Type t = GetMyType();
bool isEnum = t.IsEnum; //Type member
bool isNullableEnum = t.IsNullableEnum(); How to implement this extension method?
    public static bool IsNullableEnum(this Type t)
{
    Type u = Nullable.GetUnderlyingType(t);
    return (u != null) && u.IsEnum;
}
    EDITAR: Voy a dejar esta respuesta ya que funcionará, y demuestra algunas llamadas que los lectores quizás no conozcan. Sin embargo, la respuesta de Luke es definitivamente mejor: ve a votar :)
Tu puedes hacer:
public static bool IsNullableEnum(this Type t)
{
    return t.IsGenericType &&
           t.GetGenericTypeDefinition() == typeof(Nullable<>) &&
           t.GetGenericArguments()[0].IsEnum;
}
    
A partir de C # 6.0, la respuesta aceptada se puede refactorizar como
Nullable.GetUnderlyingType(t)?.IsEnum == true¿Se necesita == verdadero para convertir bool? bool
fuente
public static bool IsNullable(this Type type) { return type.IsClass || (type.IsGeneric && type.GetGenericTypeDefinition == typeof(Nullable<>)); }Dejé fuera la
IsEnumverificación que ya hizo, ya que eso hace que este método sea más general.fuente
Ver http://msdn.microsoft.com/en-us/library/ms366789.aspx
fuente