Checking if Type instance is a nullable enum in C#

前端 未结 5 1537
忘了有多久
忘了有多久 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:25

    public static bool IsNullableEnum(this Type t)
    {
        Type u = Nullable.GetUnderlyingType(t);
        return (u != null) && u.IsEnum;
    }
    

提交回复
热议问题