首先當(dāng)然是通過GetType()方法反射獲取其類型信息,然后對其進(jìn)行分析,但是類型信息Type中并沒有簡單地給出這么一個屬性進(jìn)行判斷。
public static bool IsNumeric(this Type dataType)
{
if (dataType == null)
throw new ArgumentNullException("dataType");
return (dataType == typeof(int)
|| dataType == typeof(double)
|| dataType == typeof(long)
|| dataType == typeof(short)
|| dataType == typeof(float)
|| dataType == typeof(Int16)
|| dataType == typeof(Int32)
|| dataType == typeof(Int64)
|| dataType == typeof(uint)
|| dataType == typeof(UInt16)
|| dataType == typeof(UInt32)
|| dataType == typeof(UInt64)
|| dataType == typeof(sbyte)
|| dataType == typeof(Single)
);
}