IsNumeric

public static bool IsNumeric(this Type t)
{
	var type = t.GetTypeWithoutNullability();
	return
		type == typeof (Int16) ||
		type == typeof (Int32) ||
		type == typeof (Int64) ||
		type == typeof (UInt16) ||
		type == typeof (UInt32) ||
		type == typeof (UInt64) ||
		type == typeof (decimal) ||
		type == typeof (float) ||
		type == typeof (double);
}

public static Type GetTypeWithoutNullability(this Type t)
{
	return t.IsNullable() ? new NullableConverter(t).UnderlyingType : t;
}

public static bool IsNullable(this Type t)
{
	return t.IsGenericType &&
		   t.GetGenericTypeDefinition() == typeof (Nullable<>);
}
Example:
if (someType.IsNumeric())
{
    do something that requires type be numeric
}

Description

Returns true if the type can be considered numeric

Details

Double click on the code to select all.

 

;