前一篇發(fā)出來后引發(fā)了積極的探討,起到了拋磚引玉效果,感謝大家參與。
吐槽一下:這個問題比其看起來要難得多得多啊。
大家的討論最終還是沒有一個完全正確的答案,不過我根據(jù)討論結(jié)果總結(jié)了一個差不多算是最終版的代碼,這里分享出來,畢竟這是大家共同的智慧結(jié)晶,沒有交流和碰撞就沒有這段代碼。
首先感謝 花生!~~ 以及 NETRUBE 提出了使用 GetTypeCode() 獲取類型代碼的方式,這個比 typeof() 的性能要高,但是有一點(diǎn)局限性,后面代碼中會指出。


由 JTANS 以及 入夏 提出的 ValueType 判斷也是有意義的,但顯然僅僅做這個判斷只能確定是否為值類型,還不能確定是否為我們要的數(shù)值類型。


由 石山不高 提出 Decimal 是非基元類型,這是正確的,我們在最終代碼中對其進(jìn)行了特殊處理。

由 花生 (為什么有兩個叫花生的!(+﹏+)~)給出的代碼比較完善,是比較具有總結(jié)性的討論成果了,最接近最終版:

其存在的問題主要是 char 和 bool 類型還是會被當(dāng)做數(shù)值,以及判斷順序需要小幅優(yōu)化。
除了對上述存在問題的改進(jìn),還重新調(diào)整為3個方法,分別是用來判斷是否為數(shù)值類型、可空數(shù)值類型及可空類型。
/// <summary> /// 判斷是否為數(shù)值類型。 /// </summary> /// <param name="t">要判斷的類型</param> /// <returns>是否為數(shù)值類型</returns> public static bool IsNumericType(this Type t) { var tc = Type.GetTypeCode(t); return (t.IsPRimitive && t.IsValueType && !t.IsEnum && tc != TypeCode.Char && tc != TypeCode.Boolean) || tc == TypeCode.Decimal; } /// <summary> /// 判斷是否為可空數(shù)值類型。 /// </summary> /// <param name="t">要判斷的類型</param> /// <returns>是否為可空數(shù)值類型</returns> public static bool IsNumericOrNullableNumericType(this Type t) { return t.IsNumericType() || (t.IsNullableType() && t.GetGenericArguments()[0].IsNumericType()); } /// <summary> /// 判斷是否為可空類型。 /// 注意,直接調(diào)用可空對象的.GetType()方法返回的會是其泛型值的實(shí)際類型,用其進(jìn)行此判斷肯定返回false。 /// </summary> /// <param name="t">要判斷的類型</param> /// <returns>是否為可空類型</returns> public static bool IsNullableType(this Type t) { return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>); }使用這個測試代碼跑可以通過,基本涵蓋了常用類型。
[TestClass] public class BasicTest { [TestMethod] public void 數(shù)值類型判斷測試() { for (int i = 0; i < 500000; i++) { Assert.IsTrue((591).GetType().IsNumericType()); Assert.IsTrue((31.131).GetType().IsNumericType()); Assert.IsTrue((31.131f).GetType().IsNumericType()); Assert.IsTrue(((Int64)31).GetType().IsNumericType()); Assert.IsTrue((new decimal(31.351)).GetType().IsNumericType()); Assert.IsTrue((new Decimal(31.351)).GetType().IsNumericType()); Assert.IsTrue(((byte)31).GetType().IsNumericType()); Assert.IsTrue(((UInt64)31).GetType().IsNumericType()); Assert.IsTrue(((UIntPtr)31).GetType().IsNumericType()); Assert.IsTrue(((short)31).GetType().IsNumericType()); Assert.IsTrue(((Single)31).GetType().IsNumericType()); Assert.IsTrue((typeof(Int64?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(UInt64?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(decimal?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(Decimal?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(UIntPtr?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(byte?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(Single?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(Double?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(float?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(double?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(int?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(short?)).IsNumericOrNullableNumericType()); Assert.IsTrue((typeof(Nullable<Byte>)).IsNumericOrNullableNumericType()); Assert.IsFalse(DateTime.Now.GetType().IsNumericType()); Assert.IsFalse(TimeSpan.FromDays(2).GetType().IsNumericType()); Assert.IsFalse("aacc".GetType().IsNumericType()); Assert.IsFalse(System.Uripartial.Path.GetType().IsNumericType()); Assert.IsFalse('c'.GetType().IsNumericType()); Assert.IsFalse(false.GetType().IsNumericType()); Assert.IsFalse((typeof(DateTime?)).IsNumericOrNullableNumericType()); Assert.IsFalse((typeof(Char?)).IsNumericOrNullableNumericType()); Assert.IsFalse((typeof(char?)).IsNumericOrNullableNumericType()); Assert.IsFalse((typeof(System.UriPartial?)).IsNumericOrNullableNumericType()); Assert.IsFalse((typeof(Boolean?)).IsNumericOrNullableNumericType()); Assert.IsFalse((typeof(bool?)).IsNumericOrNullableNumericType()); } } }需指出的是:
這里對可空類型判斷沒有使用 GetType() 方法獲取類型對象,因?yàn)槲覝y試了一下,可空類型執(zhí)行 GetType() 返回的仍然是不可空的原類型,直接進(jìn)行判斷是否為數(shù)值類型即可。
那么為什么還要做針對可空類型的判斷呢?如果你試過在 asp.net Mvc 中獲取到模型屬性的 ModelMetadata 你就會知道,其 ModelType 屬性返回的就是 Nullable<> 類型,可空類型的判斷就是給這種情況使用的。

JEFFERY YOU 提出應(yīng)該做一個測試,確實(shí)數(shù)據(jù)最有說服力。
我們就以上面的測試代碼來跑,注意這是循環(huán)五十萬輪的測試,每輪執(zhí)行該方法36次,共計(jì)執(zhí)行一千八百萬次,我們讓代碼連續(xù)跑三遍,取第三遍的時間結(jié)果(第一遍的包含初始化流程,肯定會慢一些)。
我們的代碼測試結(jié)果:

可以看出這個效率還是蠻高的,平均每輪耗時:0.016546毫秒,平均每次執(zhí)行方法耗時:0.0004596111111毫秒
然后我們把老外的代碼拿過來看一下,它跑不通這個測試,因?yàn)橐韵骂愋退鼪]做判斷:Decimal、Byte、UIntPtr 。
還有個我們測試代碼之外的 IntPtr 。
加上這些類型的判斷之后,主體方法代碼如下:
return t == typeof(int) || t == typeof(double) || t == typeof(long) || t == typeof(short) || t == typeof(float) || t == typeof(Int16) || t == typeof(Int32) || t == typeof(Int64) || t == typeof(uint) || t == typeof(UInt16) || t == typeof(UInt32) || t == typeof(UInt64) || t == typeof(sbyte) || t == typeof(Single) || t == typeof(Decimal) || t == typeof(Byte) || t == typeof(UIntPtr) || t == typeof(IntPtr);
老外的代碼測試結(jié)果:
新聞熱點(diǎn)
疑難解答
圖片精選