c# - 检查 Type 实例是否为 C# 中的可空枚举

标签 c# enums nullable

我如何在 C# 中检查类型是否为可为 null 的枚举 像

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;
}

关于c# - 检查 Type 实例是否为 C# 中的可空枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2723048/

相关文章:

c# - 具有多个值的 ArrayList c#

c++ - 如何在没有显式强制转换的情况下将 C++ 枚举类枚举器用作 std::array 索引

c# - MVC5 : Enum radio button with label as displayname

c# - 如何在 Dapper 中使用泛型和 Nullable<T> 类型进行物化?

kotlin - 菊花链 Elvis 运营商?

C# : Store Generic List<> Objects to File Stream

C#-位图到字节数组

c# - 使用 Reactive Extensions 进行数据库轮询

c# - 枚举迭代?

c# - 实例化可空值类型时调用的构造函数