Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- TypeCode typeCode = Type.GetTypeCode(desiredType);
- if (typeCode == TypeCode.Double || typeCode == TypeCode.Integer || ...)
- return true;
- /// <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 static 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( Int16[] ) ) );
- Assert.False( TypeHelper.IsNumericType( typeof( Int32[] ) ) );
- Assert.False( TypeHelper.IsNumericType( typeof( Int64[] ) ) );
- Assert.False( TypeHelper.IsNumericType( typeof( SByte[] ) ) );
- Assert.False( TypeHelper.IsNumericType( typeof( Single[] ) ) );
- Assert.False( TypeHelper.IsNumericType( typeof( UInt16[] ) ) );
- Assert.False( TypeHelper.IsNumericType( typeof( UInt32[] ) ) );
- Assert.False( TypeHelper.IsNumericType( typeof( UInt64[] ) ) );
- // 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( Int16 ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( Int32 ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( Int64 ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( SByte ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( Single ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( UInt16 ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( UInt32 ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( UInt64 ) ) );
- // 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( Int16? ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( Int32? ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( Int64? ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( SByte? ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( Single? ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( UInt16? ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( UInt32? ) ) );
- Assert.True( TypeHelper.IsNumericType( typeof( UInt64? ) ) );
- // 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( "".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( ( ( Int16 )2 ).GetType() ) );
- Assert.True( TypeHelper.IsNumericType( ( ( Int32 )2 ).GetType() ) );
- Assert.True( TypeHelper.IsNumericType( ( ( Int64 )2 ).GetType() ) );
- Assert.True( TypeHelper.IsNumericType( ( ( SByte )2 ).GetType() ) );
- Assert.True( TypeHelper.IsNumericType( 2f.GetType() ) );
- Assert.True( TypeHelper.IsNumericType( ( ( UInt16 )2 ).GetType() ) );
- Assert.True( TypeHelper.IsNumericType( ( ( UInt32 )2 ).GetType() ) );
- Assert.True( TypeHelper.IsNumericType( ( ( UInt64 )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 getttype - 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() ) );
- Int16? nullableInt16 = 12;
- Assert.True( TypeHelper.IsNumericType( nullableInt16.GetType() ) );
- Int16? nullableInt32 = 12;
- Assert.True( TypeHelper.IsNumericType( nullableInt32.GetType() ) );
- Int16? nullableInt64 = 12;
- Assert.True( TypeHelper.IsNumericType( nullableInt64.GetType() ) );
- SByte? nullableSByte = 12;
- Assert.True( TypeHelper.IsNumericType( nullableSByte.GetType() ) );
- Single? nullableSingle = 3.2f;
- Assert.True( TypeHelper.IsNumericType( nullableSingle.GetType() ) );
- UInt16? nullableUInt16 = 12;
- Assert.True( TypeHelper.IsNumericType( nullableUInt16.GetType() ) );
- UInt16? nullableUInt32 = 12;
- Assert.True( TypeHelper.IsNumericType( nullableUInt32.GetType() ) );
- UInt16? nullableUInt64 = 12;
- Assert.True( TypeHelper.IsNumericType( nullableUInt64.GetType() ) );
- }
- if (Microsoft.VisualBasic.Information.IsNumeric("5"))
- {
- //Do Something
- }
- 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;
- }
- /// <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;
- }
- ''// Return true if a type is a numeric type.
- Private Function IsNumericType(ByVal this As Type) As Boolean
- ''// All the numeric types have bits 11xx set whereas non numeric do not.
- ''// That is if you include char type which is 4(decimal) = 100(binary).
- If this.IsArray Then Return False
- If (Type.GetTypeCode(this) And &HC) > 0 Then Return True
- Return False
- End Function
- public bool IsInteger(Type t)
- {
- int i;
- return t.IsValueType && int.TryParse(t.ToString(), out i);
- }
Add Comment
Please, Sign In to add comment