在 C# 中检查 Type 实例是否为可为空的枚举 [英] Checking if Type instance is a nullable enum in C#
本文介绍了在 C# 中检查 Type 实例是否为可为空的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 C# 中检查 Type 是否为可为空的枚举类似的东西
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.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 实例是否为可为空的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文