Nullable.GetUnderlyingType(t);

看不懂单词名又看不懂文档的方法。

文档
中文版文档
a closed generic nullable type这个翻译过来也怪怪的,毕竟机器翻译。搞得我觉得自己没学过中文。

How to: Identify a Nullable Type (C# Programming Guide)
里面一句话让我好像有点理解underlying了

However, if you try to obtain type information from Nullable variables at runtime by using the GetType method or the is
operator, the result is a Type object that represents the underlying type, not the Nullable type itself.

看同事代码是这样的
Type u = Nullable.GetUnderlyingType(t); return (u != null) && u.IsEnum;

return a && b; === if (a) return b; else return a;

按上面那句话我觉得既然GetType能直接拿到underlyingType为什么要用Nullable.GetUnderlyingType(Type)来做呢,然后我尝试改了下,报错“possible mistake call to GetType()”,搞得我也是一脸懵,同事的不报错,难道是这样更安全?
原因是:nullable的变量可以为null,这时候getType会抛出NullReferenceException

stackoverflow的一个getType的问题
20170208:昨天一定是脑子瓦特了,上面这个链接就清清楚楚得说了为毛。

ps:今天看到resharper的一个decompile工具,简直太帅气了,但是我怂,不敢用,哈哈哈

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容