使用.Net,如何确定类型是否为数值类型?
但这里有一个例子:使用.Net,如何确定类型是否为数值类型?,.net,reflection,.net,Reflection,但这里有一个例子: Dim desiredType as Type if IsNumeric(desiredType) then ... 编辑:我只知道类型,不知道字符串形式的值 好的,很不幸,我必须循环使用类型代码 但这是一个很好的方法: if ((desiredType.IsArray)) return 0; switch (Type.GetTypeCode(desiredType)) { case 3: case 6: case 7
Dim desiredType as Type
if IsNumeric(desiredType) then ...
编辑:我只知道类型,不知道字符串形式的值
好的,很不幸,我必须循环使用类型代码
但这是一个很好的方法:
if ((desiredType.IsArray))
return 0;
switch (Type.GetTypeCode(desiredType))
{
case 3:
case 6:
case 7:
case 9:
case 11:
case 13:
case 14:
case 15:
return 1;
}
;return 0;
您可以使用
Type.GetTypeCode()
方法确定变量是否为数字:
TypeCode typeCode = Type.GetTypeCode(desiredType);
if (typeCode == TypeCode.Double || typeCode == TypeCode.Integer || ...)
return true;
您需要完成“…”部分中所有可用的数字类型;)
这里有更多详细信息:这里有一篇很棒的文章
选项1:
public static bool Isumeric (object Expression)
{
bool f;
ufloat64 a;
long l;
IConvertible iConvertible = null;
if ( ((Expression is IConvertible)))
{
iConvertible = (IConvertible) Expression;
}
if (iConvertible == null)
{
if ( ((Expression is char[])))
{
Expression = new String ((char[]) Expression);
goto IL_002d; // hopefully inserted by optimizer
}
return 0;
}
IL_002d:
TypeCode typeCode = iConvertible.GetTypeCode ();
if ((typeCode == 18) || (typeCode == 4))
{
string str = iConvertible.ToString (null);
try
{
if ( (StringType.IsHexOrOctValue (str, l)))
{
f = true;
return f;
}
}
catch (Exception )
{
f = false;
return f;
};
return DoubleType.TryParse (str, a);
}
return Utils.IsNumericTypeCode (typeCode);
}
internal static bool IsNumericType (Type typ)
{
bool f;
TypeCode typeCode;
if ( (typ.IsArray))
{
return 0;
}
switch (Type.GetTypeCode (typ))
{
case 3:
case 6:
case 7:
case 9:
case 11:
case 13:
case 14:
case 15:
return 1;
};
return 0;
}
请参考Microsoft.VisualBasic.dll,然后执行以下操作:
if (Microsoft.VisualBasic.Information.IsNumeric("5"))
{
//Do Something
}
选项2:
public static bool Isumeric (object Expression)
{
bool f;
ufloat64 a;
long l;
IConvertible iConvertible = null;
if ( ((Expression is IConvertible)))
{
iConvertible = (IConvertible) Expression;
}
if (iConvertible == null)
{
if ( ((Expression is char[])))
{
Expression = new String ((char[]) Expression);
goto IL_002d; // hopefully inserted by optimizer
}
return 0;
}
IL_002d:
TypeCode typeCode = iConvertible.GetTypeCode ();
if ((typeCode == 18) || (typeCode == 4))
{
string str = iConvertible.ToString (null);
try
{
if ( (StringType.IsHexOrOctValue (str, l)))
{
f = true;
return f;
}
}
catch (Exception )
{
f = false;
return f;
};
return DoubleType.TryParse (str, a);
}
return Utils.IsNumericTypeCode (typeCode);
}
internal static bool IsNumericType (Type typ)
{
bool f;
TypeCode typeCode;
if ( (typ.IsArray))
{
return 0;
}
switch (Type.GetTypeCode (typ))
{
case 3:
case 6:
case 7:
case 9:
case 11:
case 13:
case 14:
case 15:
return 1;
};
return 0;
}
使用Type.IsValueType()和TryParse():
这里晚了几年,但这是我的解决方案(您可以选择是否包含布尔值)。解决可为空的情况。包括XUnit测试
/// <summary>
/// Determines if a type is numeric. Nullable numeric types are considered numeric.
/// </summary>
/// <remarks>
/// Boolean is not considered numeric.
/// </remarks>
public static bool IsNumericType( Type type )
{
if (type == null)
{
return false;
}
switch (Type.GetTypeCode(type))
{
case TypeCode.Byte:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return true;
case TypeCode.Object:
if ( type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return IsNumericType(Nullable.GetUnderlyingType(type));
}
return false;
}
return false;
}
/// <summary>
/// Tests the IsNumericType method.
/// </summary>
[Fact]
public void IsNumericTypeTest()
{
// Non-numeric types
Assert.False(TypeHelper.IsNumericType(null));
Assert.False(TypeHelper.IsNumericType(typeof(object)));
Assert.False(TypeHelper.IsNumericType(typeof(DBNull)));
Assert.False(TypeHelper.IsNumericType(typeof(bool)));
Assert.False(TypeHelper.IsNumericType(typeof(char)));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime)));
Assert.False(TypeHelper.IsNumericType(typeof(string)));
// Arrays of numeric and non-numeric types
Assert.False(TypeHelper.IsNumericType(typeof(object[])));
Assert.False(TypeHelper.IsNumericType(typeof(DBNull[])));
Assert.False(TypeHelper.IsNumericType(typeof(bool[])));
Assert.False(TypeHelper.IsNumericType(typeof(char[])));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime[])));
Assert.False(TypeHelper.IsNumericType(typeof(string[])));
Assert.False(TypeHelper.IsNumericType(typeof(byte[])));
Assert.False(TypeHelper.IsNumericType(typeof(decimal[])));
Assert.False(TypeHelper.IsNumericType(typeof(double[])));
Assert.False(TypeHelper.IsNumericType(typeof(short[])));
Assert.False(TypeHelper.IsNumericType(typeof(int[])));
Assert.False(TypeHelper.IsNumericType(typeof(long[])));
Assert.False(TypeHelper.IsNumericType(typeof(sbyte[])));
Assert.False(TypeHelper.IsNumericType(typeof(float[])));
Assert.False(TypeHelper.IsNumericType(typeof(ushort[])));
Assert.False(TypeHelper.IsNumericType(typeof(uint[])));
Assert.False(TypeHelper.IsNumericType(typeof(ulong[])));
// numeric types
Assert.True(TypeHelper.IsNumericType(typeof(byte)));
Assert.True(TypeHelper.IsNumericType(typeof(decimal)));
Assert.True(TypeHelper.IsNumericType(typeof(double)));
Assert.True(TypeHelper.IsNumericType(typeof(short)));
Assert.True(TypeHelper.IsNumericType(typeof(int)));
Assert.True(TypeHelper.IsNumericType(typeof(long)));
Assert.True(TypeHelper.IsNumericType(typeof(sbyte)));
Assert.True(TypeHelper.IsNumericType(typeof(float)));
Assert.True(TypeHelper.IsNumericType(typeof(ushort)));
Assert.True(TypeHelper.IsNumericType(typeof(uint)));
Assert.True(TypeHelper.IsNumericType(typeof(ulong)));
// Nullable non-numeric types
Assert.False(TypeHelper.IsNumericType(typeof(bool?)));
Assert.False(TypeHelper.IsNumericType(typeof(char?)));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime?)));
// Nullable numeric types
Assert.True(TypeHelper.IsNumericType(typeof(byte?)));
Assert.True(TypeHelper.IsNumericType(typeof(decimal?)));
Assert.True(TypeHelper.IsNumericType(typeof(double?)));
Assert.True(TypeHelper.IsNumericType(typeof(short?)));
Assert.True(TypeHelper.IsNumericType(typeof(int?)));
Assert.True(TypeHelper.IsNumericType(typeof(long?)));
Assert.True(TypeHelper.IsNumericType(typeof(sbyte?)));
Assert.True(TypeHelper.IsNumericType(typeof(float?)));
Assert.True(TypeHelper.IsNumericType(typeof(ushort?)));
Assert.True(TypeHelper.IsNumericType(typeof(uint?)));
Assert.True(TypeHelper.IsNumericType(typeof(ulong?)));
// Testing with GetType because of handling with non-numerics. See:
// http://msdn.microsoft.com/en-us/library/ms366789.aspx
// Using GetType - non-numeric
Assert.False(TypeHelper.IsNumericType((new object()).GetType()));
Assert.False(TypeHelper.IsNumericType(DBNull.Value.GetType()));
Assert.False(TypeHelper.IsNumericType(true.GetType()));
Assert.False(TypeHelper.IsNumericType('a'.GetType()));
Assert.False(TypeHelper.IsNumericType((new DateTime(2009, 1, 1)).GetType()));
Assert.False(TypeHelper.IsNumericType(string.Empty.GetType()));
// Using GetType - numeric types
// ReSharper disable RedundantCast
Assert.True(TypeHelper.IsNumericType((new byte()).GetType()));
Assert.True(TypeHelper.IsNumericType(43.2m.GetType()));
Assert.True(TypeHelper.IsNumericType(43.2d.GetType()));
Assert.True(TypeHelper.IsNumericType(((short)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((int)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((long)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((sbyte)2).GetType()));
Assert.True(TypeHelper.IsNumericType(2f.GetType()));
Assert.True(TypeHelper.IsNumericType(((ushort)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((uint)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((ulong)2).GetType()));
// ReSharper restore RedundantCast
// Using GetType - nullable non-numeric types
bool? nullableBool = true;
Assert.False(TypeHelper.IsNumericType(nullableBool.GetType()));
char? nullableChar = ' ';
Assert.False(TypeHelper.IsNumericType(nullableChar.GetType()));
DateTime? nullableDateTime = new DateTime(2009, 1, 1);
Assert.False(TypeHelper.IsNumericType(nullableDateTime.GetType()));
// Using GetType - nullable numeric types
byte? nullableByte = 12;
Assert.True(TypeHelper.IsNumericType(nullableByte.GetType()));
decimal? nullableDecimal = 12.2m;
Assert.True(TypeHelper.IsNumericType(nullableDecimal.GetType()));
double? nullableDouble = 12.32;
Assert.True(TypeHelper.IsNumericType(nullableDouble.GetType()));
short? nullableInt16 = 12;
Assert.True(TypeHelper.IsNumericType(nullableInt16.GetType()));
short? nullableInt32 = 12;
Assert.True(TypeHelper.IsNumericType(nullableInt32.GetType()));
short? nullableInt64 = 12;
Assert.True(TypeHelper.IsNumericType(nullableInt64.GetType()));
sbyte? nullableSByte = 12;
Assert.True(TypeHelper.IsNumericType(nullableSByte.GetType()));
float? nullableSingle = 3.2f;
Assert.True(TypeHelper.IsNumericType(nullableSingle.GetType()));
ushort? nullableUInt16 = 12;
Assert.True(TypeHelper.IsNumericType(nullableUInt16.GetType()));
ushort? nullableUInt32 = 12;
Assert.True(TypeHelper.IsNumericType(nullableUInt32.GetType()));
ushort? nullableUInt64 = 12;
Assert.True(TypeHelper.IsNumericType(nullableUInt64.GetType()));
}
//
///确定类型是否为数字。可为空的数值类型被视为数值类型。
///
///
///布尔值不被视为数字。
///
公共静态bool IsNumericType(类型)
{
if(type==null)
{
返回false;
}
开关(类型.GetTypeCode(类型))
{
大小写类型代码。字节:
大小写类型代码。十进制:
案例类型代码。双:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
案例类型代码.SByte:
案例类型代码。单个:
案例类型代码.UInt16:
案例类型代码.UInt32:
案例类型代码.UInt64:
返回true;
案例类型代码。对象:
if(type.IsGenericType&&type.GetGenericTypeDefinition()==typeof(可为空))
{
返回IsNumericType(null.getUnderlineType(type));
}
返回false;
}
返回false;
}
///
///测试IsNumericType方法。
///
[事实]
public void IsNumericTypeTest()
{
//非数字类型
Assert.False(TypeHelper.IsNumericType(null));
Assert.False(TypeHelper.IsNumericType(typeof(object));
Assert.False(TypeHelper.IsNumericType(typeof(DBNull));
Assert.False(TypeHelper.IsNumericType(typeof(bool));
Assert.False(TypeHelper.IsNumericType(typeof(char)));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime));
Assert.False(TypeHelper.IsNumericType(typeof(string));
//数字和非数字类型的数组
Assert.False(TypeHelper.IsNumericType(typeof(object[])));
Assert.False(TypeHelper.IsNumericType(typeof(DBNull[])));
Assert.False(TypeHelper.IsNumericType(typeof(bool[])));
Assert.False(TypeHelper.IsNumericType(typeof(char[])));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime[])));
Assert.False(TypeHelper.IsNumericType(typeof(string[])));
Assert.False(TypeHelper.IsNumericType(typeof(byte[]));
Assert.False(TypeHelper.IsNumericType(typeof(decimal[])));
Assert.False(TypeHelper.IsNumericType(typeof(double[])));
Assert.False(TypeHelper.IsNumericType(typeof(short[])));
Assert.False(TypeHelper.IsNumericType(typeof(int[]));
Assert.False(TypeHelper.IsNumericType(typeof(long[])));
Assert.False(TypeHelper.IsNumericType(typeof(sbyte[])));
Assert.False(TypeHelper.IsNumericType(typeof(float[])));
Assert.False(TypeHelper.IsNumericType(typeof(ushort[])));
Assert.False(TypeHelper.IsNumericType(typeof(uint[])));
Assert.False(TypeHelper.IsNumericType(typeof(ulong[])));
//数字类型
True(TypeHelper.IsNumericType(typeof(byte));
True(TypeHelper.IsNumericType(typeof(decimal));
True(TypeHelper.IsNumericType(typeof(double));
True(TypeHelper.IsNumericType(typeof(short));
True(TypeHelper.IsNumericType(typeof(int));
True(TypeHelper.IsNumericType(typeof(long));
True(TypeHelper.IsNumericType(typeof(sbyte));
True(TypeHelper.IsNumericType(typeof(float));
True(TypeHelper.IsNumericType(typeof(ushort));
True(TypeHelper.IsNumericType(typeof(uint));
True(TypeHelper.IsNumericType(typeof(ulong));
//可为空的非数字类型
Assert.False(TypeHelper.IsNumericType(typeof(bool?)));
Assert.False(TypeHelper.IsNumericType(typeof(char?));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime?)));
//可为空的数字类型
True(TypeHelper.IsNumericType(typeof(byte?));
True(TypeHelper.IsNumericType(typeof(decimal?));
True(TypeHelper.IsNumericType(typeof(double?));
True(TypeHelper.IsNumericType(typeof(short?));
True(TypeHelper.IsNumericType(typeof(int?));
True(TypeHelper.IsNumericType(typeof(long?));
True(TypeHelper.IsNumericType(typeof(sbyte?));
True(TypeHelper.IsNumericType(typeof(float?));
True(TypeHelper.IsNumericType(typeof(ushort?));
True(TypeHelper.IsNumericType(typeof(uint?));
True(TypeHelper.IsNumericType(typeof(ulong?));
//由于使用非数字进行处理,因此使用GetType进行测试。请参阅:
// http://msdn.microsoft.com/en-us/library/ms366789.aspx
//使用GetType-非数字
Assert.False(TypeHelper.IsNumericType((新对象()).GetType());
False(TypeHelper.IsNumericType(DBNull.Value.GetType());
Assert.False(TypeHelper.IsNumericType(true.GetType());
Assert.False(TypeHelper.IsNumericType('a'.GetType());
Assert.False(TypeHelper.IsNumericType((newdatetime(2009,1,1)).GetType());
Assert.False(TypeHelper.IsNumericType(string.Empty.GetType());
//使用GetType-numeric类型
//重拾器禁用冗余广播
True(TypeHelper.IsNumericType((新字节()).GetType());
True(TypeHelper.IsNumericType(43.2m.GetType());
True(TypeHelper.IsNumericType(43.2d.GetType());
True(TypeHelper.IsNumericType(((short)2.GetType());
True(TypeHelper.IsNumericType((int)2.GetType());
True(TypeHelper.IsNumericType(((long)2.GetType());
Assert.True(TypeHelper.IsNumericType)((s
/// <summary>
/// Determines if a type is numeric. Nullable numeric types are considered numeric.
/// </summary>
/// <remarks>
/// Boolean is not considered numeric.
/// </remarks>
public static bool IsNumericType( Type type )
{
if (type == null)
{
return false;
}
switch (Type.GetTypeCode(type))
{
case TypeCode.Byte:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return true;
case TypeCode.Object:
if ( type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return IsNumericType(Nullable.GetUnderlyingType(type));
}
return false;
}
return false;
}
/// <summary>
/// Tests the IsNumericType method.
/// </summary>
[Fact]
public void IsNumericTypeTest()
{
// Non-numeric types
Assert.False(TypeHelper.IsNumericType(null));
Assert.False(TypeHelper.IsNumericType(typeof(object)));
Assert.False(TypeHelper.IsNumericType(typeof(DBNull)));
Assert.False(TypeHelper.IsNumericType(typeof(bool)));
Assert.False(TypeHelper.IsNumericType(typeof(char)));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime)));
Assert.False(TypeHelper.IsNumericType(typeof(string)));
// Arrays of numeric and non-numeric types
Assert.False(TypeHelper.IsNumericType(typeof(object[])));
Assert.False(TypeHelper.IsNumericType(typeof(DBNull[])));
Assert.False(TypeHelper.IsNumericType(typeof(bool[])));
Assert.False(TypeHelper.IsNumericType(typeof(char[])));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime[])));
Assert.False(TypeHelper.IsNumericType(typeof(string[])));
Assert.False(TypeHelper.IsNumericType(typeof(byte[])));
Assert.False(TypeHelper.IsNumericType(typeof(decimal[])));
Assert.False(TypeHelper.IsNumericType(typeof(double[])));
Assert.False(TypeHelper.IsNumericType(typeof(short[])));
Assert.False(TypeHelper.IsNumericType(typeof(int[])));
Assert.False(TypeHelper.IsNumericType(typeof(long[])));
Assert.False(TypeHelper.IsNumericType(typeof(sbyte[])));
Assert.False(TypeHelper.IsNumericType(typeof(float[])));
Assert.False(TypeHelper.IsNumericType(typeof(ushort[])));
Assert.False(TypeHelper.IsNumericType(typeof(uint[])));
Assert.False(TypeHelper.IsNumericType(typeof(ulong[])));
// numeric types
Assert.True(TypeHelper.IsNumericType(typeof(byte)));
Assert.True(TypeHelper.IsNumericType(typeof(decimal)));
Assert.True(TypeHelper.IsNumericType(typeof(double)));
Assert.True(TypeHelper.IsNumericType(typeof(short)));
Assert.True(TypeHelper.IsNumericType(typeof(int)));
Assert.True(TypeHelper.IsNumericType(typeof(long)));
Assert.True(TypeHelper.IsNumericType(typeof(sbyte)));
Assert.True(TypeHelper.IsNumericType(typeof(float)));
Assert.True(TypeHelper.IsNumericType(typeof(ushort)));
Assert.True(TypeHelper.IsNumericType(typeof(uint)));
Assert.True(TypeHelper.IsNumericType(typeof(ulong)));
// Nullable non-numeric types
Assert.False(TypeHelper.IsNumericType(typeof(bool?)));
Assert.False(TypeHelper.IsNumericType(typeof(char?)));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime?)));
// Nullable numeric types
Assert.True(TypeHelper.IsNumericType(typeof(byte?)));
Assert.True(TypeHelper.IsNumericType(typeof(decimal?)));
Assert.True(TypeHelper.IsNumericType(typeof(double?)));
Assert.True(TypeHelper.IsNumericType(typeof(short?)));
Assert.True(TypeHelper.IsNumericType(typeof(int?)));
Assert.True(TypeHelper.IsNumericType(typeof(long?)));
Assert.True(TypeHelper.IsNumericType(typeof(sbyte?)));
Assert.True(TypeHelper.IsNumericType(typeof(float?)));
Assert.True(TypeHelper.IsNumericType(typeof(ushort?)));
Assert.True(TypeHelper.IsNumericType(typeof(uint?)));
Assert.True(TypeHelper.IsNumericType(typeof(ulong?)));
// Testing with GetType because of handling with non-numerics. See:
// http://msdn.microsoft.com/en-us/library/ms366789.aspx
// Using GetType - non-numeric
Assert.False(TypeHelper.IsNumericType((new object()).GetType()));
Assert.False(TypeHelper.IsNumericType(DBNull.Value.GetType()));
Assert.False(TypeHelper.IsNumericType(true.GetType()));
Assert.False(TypeHelper.IsNumericType('a'.GetType()));
Assert.False(TypeHelper.IsNumericType((new DateTime(2009, 1, 1)).GetType()));
Assert.False(TypeHelper.IsNumericType(string.Empty.GetType()));
// Using GetType - numeric types
// ReSharper disable RedundantCast
Assert.True(TypeHelper.IsNumericType((new byte()).GetType()));
Assert.True(TypeHelper.IsNumericType(43.2m.GetType()));
Assert.True(TypeHelper.IsNumericType(43.2d.GetType()));
Assert.True(TypeHelper.IsNumericType(((short)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((int)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((long)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((sbyte)2).GetType()));
Assert.True(TypeHelper.IsNumericType(2f.GetType()));
Assert.True(TypeHelper.IsNumericType(((ushort)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((uint)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((ulong)2).GetType()));
// ReSharper restore RedundantCast
// Using GetType - nullable non-numeric types
bool? nullableBool = true;
Assert.False(TypeHelper.IsNumericType(nullableBool.GetType()));
char? nullableChar = ' ';
Assert.False(TypeHelper.IsNumericType(nullableChar.GetType()));
DateTime? nullableDateTime = new DateTime(2009, 1, 1);
Assert.False(TypeHelper.IsNumericType(nullableDateTime.GetType()));
// Using GetType - nullable numeric types
byte? nullableByte = 12;
Assert.True(TypeHelper.IsNumericType(nullableByte.GetType()));
decimal? nullableDecimal = 12.2m;
Assert.True(TypeHelper.IsNumericType(nullableDecimal.GetType()));
double? nullableDouble = 12.32;
Assert.True(TypeHelper.IsNumericType(nullableDouble.GetType()));
short? nullableInt16 = 12;
Assert.True(TypeHelper.IsNumericType(nullableInt16.GetType()));
short? nullableInt32 = 12;
Assert.True(TypeHelper.IsNumericType(nullableInt32.GetType()));
short? nullableInt64 = 12;
Assert.True(TypeHelper.IsNumericType(nullableInt64.GetType()));
sbyte? nullableSByte = 12;
Assert.True(TypeHelper.IsNumericType(nullableSByte.GetType()));
float? nullableSingle = 3.2f;
Assert.True(TypeHelper.IsNumericType(nullableSingle.GetType()));
ushort? nullableUInt16 = 12;
Assert.True(TypeHelper.IsNumericType(nullableUInt16.GetType()));
ushort? nullableUInt32 = 12;
Assert.True(TypeHelper.IsNumericType(nullableUInt32.GetType()));
ushort? nullableUInt64 = 12;
Assert.True(TypeHelper.IsNumericType(nullableUInt64.GetType()));
}
/// <summary>
/// Determines whether the supplied object is a .NET numeric system type
/// </summary>
/// <param name="val">The object to test</param>
/// <returns>true=Is numeric; false=Not numeric</returns>
public static bool IsNumeric(ref object val)
{
if (val == null)
return false;
// Test for numeric type, returning true if match
if
(
val is double || val is float || val is int || val is long || val is decimal ||
val is short || val is uint || val is ushort || val is ulong || val is byte ||
val is sbyte
)
return true;
// Not numeric
return false;
}
internal static bool IsNumeric(Type type)
{
type = type.GetNonNullableType();
if (!type.IsEnum)
{
switch (Type.GetTypeCode(type))
{
case TypeCode.Char:
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
return true;
}
}
return false;
}
//where GetNonNullableType is defined as
internal static Type GetNonNullableType(this Type type)
{
if (type.IsNullableType())
{
return type.GetGenericArguments()[0];
}
return type;
}
//where IsNullableType is defined as
internal static bool IsNullableType(this Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
public static class ReflectionExtensions
{
public static bool IsNullable(this Type type) {
return
type != null &&
type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
public static bool IsNumeric(this Type type) {
if (type == null || type.IsEnum)
return false;
if (IsNullable(type))
return IsNumeric(Nullable.GetUnderlyingType(type));
switch (Type.GetTypeCode(type)) {
case TypeCode.Byte:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return true;
default:
return false;
}
}
}
public static bool IsNumeric(Type type)
{
var t = Nullable.GetUnderlyingType(type) ?? type;
return t.IsPrimitive || t == typeof(decimal);
}
return (t.IsPrimitive || t == typeof(decimal)) && t != typeof(char);
public static bool IsNumeric<T>(this T value)
{
var t = Nullable.GetUnderlyingType(value.GetType()) ?? value.GetType();
return t.IsPrimitive || t == typeof(decimal);
}
typeof(decimal?).IsNumericType()