[csharp] bool IsNumeric(Type type)

时间:2023-03-09 17:06:38
[csharp] bool IsNumeric(Type type)
 /*
"C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe" /out:IsNumericType.exe IsNumericType.cs && start "IsNumericType.exe" IsNumericType.exe
IsNumeric(System.Boolean) -> False
IsNumeric(System.String) -> False
IsNumeric(System.Char) -> False
IsNumeric(System.Byte) -> True
IsNumeric(System.Byte[]) -> False
IsNumeric(System.DateTime) -> False
IsNumeric(System.Int32) -> True
IsNumeric(System.Single) -> True
IsNumeric(System.Decimal) -> True
IsNumeric(System.DayOfWeek) -> True
IsNumeric(System.Guid) -> False
IsNumeric(System.IntPtr) -> False
IsNumeric(System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=ne
utral, PublicKeyToken=b77a5c561934e089]]) -> False
IsNumeric(System.Action) -> False
Press any key to EXIT...
*/
using System;
using System.Reflection; static class Program {
static bool IsNumeric(Type type) {
switch (Type.GetTypeCode(type)) {
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return true;
default:
return false;
}
} public static void Main() {
Test(typeof(bool));
Test(typeof(string));
Test(typeof(char));
Test(typeof(byte));
Test(typeof(byte[]));
Test(typeof(DateTime));
Test(typeof(int));
Test(typeof(float));
Test(typeof(Decimal));
Test(typeof(DayOfWeek));
Test(typeof(Guid));
Test(typeof(IntPtr));
Test(typeof(int?));
Test(typeof(Action));
Console.Write("Press any key to EXIT...");
Console.ReadKey(true);
} static void Test(Type type) {
Console.WriteLine("IsNumeric({0}) -> {1}", type.FullName, IsNumeric(type));
} }