I know this is a VERY late answer, but here is the function I use:
public static bool IsNumeric(Type type) { var t = Nullable.GetUnderlyingType(type) ?? type; return t.IsPrimitive || t == typeof(decimal); }
If you want to exclude char as a numeric type, you can use this example:
return (t.IsPrimitive || t == typeof(decimal)) && t != typeof(char);
According to MSDN :
Primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double and Single.
Note. This check includes IntPtr and UIntPtr .
Here is the same function as the general extension method (I know this does not work for the OP case, but it may seem useful to someone):
public static bool IsNumeric<T>(this T value) { var t = Nullable.GetUnderlyingType(value.GetType()) ?? value.GetType(); return t.IsPrimitive || t == typeof(decimal); }
Josh T. Mar 07 '17 at 17:18 2017-03-07 17:18
source share