Checking if Type instance is a nullable enum in C#

前端 未结 5 1540
忘了有多久
忘了有多久 2020-12-13 12:05

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.IsNullab         


        
5条回答
  •  感动是毒
    2020-12-13 12:31

    public static bool IsNullable(this Type type)
    {
        return type.IsClass
            || (type.IsGeneric && type.GetGenericTypeDefinition == typeof(Nullable<>));
    }
    

    I left out the IsEnum check you already made, as that makes this method more general.

提交回复
热议问题