.NET:注意 Primitive 这个小坑

背景

有个需求,需要递归遍历类型的所有属性(属性的属性),然后对不同的类型做不同的处理,或者只是将类型分为三类:Primitive、Complex 和 Collection。因为 MS 的 Type 提供了 IsPrimitive 属性,这里重点说下我对 Primitive 的期望,原本以为肯定要包含:Decimal,有可能包含:String,可是并非如此。

Type.IsPrimitive Property

官方地址:http://msdn.microsoft.com/en-us/library/system.type.isprimitive(v=vs.110).aspx

The primitive types are BooleanByteSByteInt16UInt16Int32UInt32Int64UInt64IntPtrUIntPtrCharDouble, and Single.

If the current Type represents a generic type, or a type parameter in the definition of a generic type or generic method, this property always returns false.

原文地址:https://www.cnblogs.com/happyframework/p/3612118.html