IsPrimitive不包括为空的基本值 [英] IsPrimitive doesn't include nullable primitive values
问题描述
我要检查,如果一个类型是基本类型或不并用下面的代码:
I want to check if a Type is primitive or not and used the following code:
return type.IsValueType && type.IsPrimitive;
这工作正常aslong作为原始心不是空。例如INT?我怎么能检查类型是可空的基本类型? (供参考:<?code> type.IsPrimitive ==虚假对INT)
This works fine aslong as the primitive isnt nullable. For example int?, how can I check if the type is a nullable primitive type? (FYI: type.IsPrimitive == false
on int?)
推荐答案
从MSDN:
的基本类型有布尔,字节,为SByte,Int16的,UINT16,的Int32,
UInt32的,Int64的, UINT64,IntPtr的,UIntPtr,煤焦,双和单
The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single.
所以基本上你应该期望可空<的Int32> ;
来的不的一个原始类型
So basically you should expect Nullable<Int32>
to not be a primitive type.
您可以使用的 Nullable.GetUnderlyingType
来提取的Int32
从可空<的Int32方式>
这篇关于IsPrimitive不包括为空的基本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!