检查 C# 中的 Type 实例是否为可空枚举
在 C# 中如何检查 Type 是否为可空枚举 类似的东西
Type t = GetMyType();
bool isEnum = t.IsEnum; //Type member
bool isNullableEnum = t.IsNullableEnum(); How to implement this extension method?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:我将保留这个答案,因为它会起作用,并且它演示了读者可能不知道的一些调用。但是,卢克的回答绝对更好 - 去投票吧:)
你可以这样做:
EDIT: I'm going to leave this answer up as it will work, and it demonstrates a few calls that readers may not otherwise know about. However, Luke's answer is definitely nicer - go upvote it :)
You can do:
从 C# 6.0 开始,接受的答案可以重构为
The == true is required to conversion bool?布尔
As from C# 6.0 the accepted answer can be refactored as
The == true is needed to convert bool? to bool
我遗漏了您已经进行的 IsEnum 检查,因为这使得该方法更加通用。
I left out the
IsEnum
check you already made, as that makes this method more general.请参阅 http://msdn.microsoft.com/en-us/library/ms366789 .aspx
See http://msdn.microsoft.com/en-us/library/ms366789.aspx