使用.NET,我怎么能确定一个类型是数字值类型? [英] Using .Net, how can I determine if a type is a Numeric ValueType?

查看:198
本文介绍了使用.NET,我怎么能确定一个类型是数字值类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但这里有一个例子:

 暗淡desiredType的类型
如果则IsNumeric(desiredType),然后...
 

编辑:我只知道是什么类型,不是值作为一个字符串

好了,遗憾的是我通过输入code具有循环。

但是,这是一个很好的方式做到这一点:

  IF((desiredType.IsArray))
      返回0;

 开关(Type.GetType code(desiredType))
 {
      案例3:
      情况6:
      案例7:
      案例9:
      案例11:
      案例13:
      案例14:
      案例15:
          返回1;
 }
 ;返回0;
 

解决方案

几年下旬在这里,但这里是我的解决方案(你可以选择是否包含布尔)。解决了可空的情况下。的xUnit测试包括

  ///<总结>
///确定一个类型是数字。可空数值类型被认为是数字。
///< /总结>
///<说明>
///布尔不被视为数字。
///< /说明>
公共静态布尔IsNumericType(类型类型)
{
    如果(类型== NULL)
    {
        返回false;
    }

    开关(Type.GetType code(类型))
    {
        案件类型code.Byte:
        案件类型code.Decimal:
        案件类型code.Double:
        案件类型code.Int16:
        案件类型code.Int32:
        案件类型code.Int64:
        案件类型code.SByte:
        案件类型code.Single:
        案件类型code.UInt16:
        案件类型code.UInt32:
        案件类型code.UInt64:
            返回true;
        案件类型code.Object:
            如果(type.IsGenericType&安培;&安培; type.GetGenericTypeDefinition()== typeof运算(可空<>))
            {
               返回IsNumericType(Nullable.GetUnderlyingType(类型));
            }
            返回false;
    }
    返回false;
}



///<总结>
///测试的IsNumericType方法。
///< /总结>
[事实]
公共无效IsNumericTypeTest()
{
    //非数值类型
    Assert.False(TypeHelper.IsNumericType(空));
    Assert.False(TypeHelper.IsNumericType(typeof运算(对象)));
    Assert.False(TypeHelper.IsNumericType(typeof运算(为DBNull)));
    Assert.False(TypeHelper.IsNumericType(typeof运算(布尔)));
    Assert.False(TypeHelper.IsNumericType(typeof运算(炭)));
    Assert.False(TypeHelper.IsNumericType(typeof运算(日期时间)));
    Assert.False(TypeHelper.IsNumericType(typeof运算(字符串)));

    //数字和非数字类型的数组
    Assert.False(TypeHelper.IsNumericType(typeof运算(对象[])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(DBNull的[])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(布尔[])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(的char [])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(DateTime的[])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(字符串[])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(字节[])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(十进制[])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(双[])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(简称[])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(INT [])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(长[])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(为sbyte [])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(浮动[])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(USHORT [])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(UINT [])));
    Assert.False(TypeHelper.IsNumericType(typeof运算(ULONG [])));

    //数字类型
    Assert.True(TypeHelper.IsNumericType(typeof运算(字节)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(十进制)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(双)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(短)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(INT)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(长)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(为sbyte)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(浮点)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(USHORT)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(UINT)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(ULONG)));

    //可空非数字类型
    Assert.False(TypeHelper.IsNumericType(typeof运算(布尔)));
    Assert.False(TypeHelper.IsNumericType(typeof运算(炭)));
    Assert.False(TypeHelper.IsNumericType(typeof运算(日期时间))?);

    //可空数值类型
    Assert.True(TypeHelper.IsNumericType(typeof运算(字节)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(十进制)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(双)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(短)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(INT)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(长)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(为sbyte)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(浮动)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(USHORT)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(UINT)));
    Assert.True(TypeHelper.IsNumericType(typeof运算(ULONG)));

    //测试用的GetType,因为处理非Numerics的。看到:
    // http://msdn.microsoft.com/en-us/library/ms366789.aspx

    //使用的GetType  - 非数字
    Assert.False(TypeHelper.IsNumericType((新的对象())的GetType())。);
    Assert.False(TypeHelper.IsNumericType(DBNull.Value.GetType()));
    Assert.False(TypeHelper.IsNumericType(true.GetType()));
    Assert.False(TypeHelper.IsNumericType('a'.GetType()));
    Assert.False(TypeHelper.IsNumericType((新日期时间(2009年,1,1))的GetType())。);
    Assert.False(TypeHelper.IsNumericType(string.Empty.GetType()));

    //使用的GetType  - 数字类型
    // ReSharper的禁用RedundantCast
    Assert.True(TypeHelper.IsNumericType((新字节())的GetType())。);
    Assert.True(TypeHelper.IsNumericType(43.2m.GetType()));
    Assert.True(TypeHelper.IsNumericType(43.2d.GetType()));
    Assert.True(TypeHelper.IsNumericType(((短)2).GetType()));
    Assert.True(TypeHelper.IsNumericType(((int)的2).GetType()));
    Assert.True(TypeHelper.IsNumericType(((长)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的恢复RedundantCast

    //使用的GetType  - 为空的非数字类型
    布尔? nullableBool = TRUE;
    Assert.False(TypeHelper.IsNumericType(nullableBool.GetType()));
    烧焦? nullableChar ='';
    Assert.False(TypeHelper.IsNumericType(nullableChar.GetType()));
    日期时间? nullableDateTime =新的日期时间(2009年,1,1);
    Assert.False(TypeHelper.IsNumericType(nullableDateTime.GetType()));

    //使用的GetType  - 可空数值类型
    字节? nullableByte = 12;
    Assert.True(TypeHelper.IsNumericType(nullableByte.GetType()));
    十进制? nullableDecimal =12.2米;
    Assert.True(TypeHelper.IsNumericType(nullableDecimal.GetType()));
    双? nullableDouble = 12.32;
    Assert.True(TypeHelper.IsNumericType(nullableDouble.GetType()));
    短? nullableInt16 = 12;
    Assert.True(TypeHelper.IsNumericType(nullableInt16.GetType()));
    短? nullableInt32 = 12;
    Assert.True(TypeHelper.IsNumericType(nullableInt32.GetType()));
    短? nullableInt64 = 12;
    Assert.True(TypeHelper.IsNumericType(nullableInt64.GetType()));
    为sbyte? nullableSByte = 12;
    Assert.True(TypeHelper.IsNumericType(nullableSByte.GetType()));
    浮动? 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()));
}
 

But here's an example:

Dim desiredType as Type
if IsNumeric(desiredType) then ...

EDIT: I only know the Type, not the Value as a string.

Ok, so unfortunately I have to cycle through the TypeCode.

But this is a nice way to do it:

 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;

解决方案

A few years late here, but here's my solution (you can choose whether to include boolean). Solves for the Nullable case. XUnit test included

/// <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()));
}

这篇关于使用.NET,我怎么能确定一个类型是数字值类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆