Checking if Type instance is a nullable enum in C#

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

    As from C# 6.0 the accepted answer can be refactored as

    Nullable.GetUnderlyingType(t)?.IsEnum == true
    

    The == true is needed to convert bool? to bool

提交回复
热议问题