[csharp] bool IsNumeric(Type type)

 1 /*
 2 "C:Program Files (x86)MSBuild14.0Bincsc.exe" /out:IsNumericType.exe IsNumericType.cs && start "IsNumericType.exe" IsNumericType.exe
 3 IsNumeric(System.Boolean) -> False
 4 IsNumeric(System.String) -> False
 5 IsNumeric(System.Char) -> False
 6 IsNumeric(System.Byte) -> True
 7 IsNumeric(System.Byte[]) -> False
 8 IsNumeric(System.DateTime) -> False
 9 IsNumeric(System.Int32) -> True
10 IsNumeric(System.Single) -> True
11 IsNumeric(System.Decimal) -> True
12 IsNumeric(System.DayOfWeek) -> True
13 IsNumeric(System.Guid) -> False
14 IsNumeric(System.IntPtr) -> False
15 IsNumeric(System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=ne
16 utral, PublicKeyToken=b77a5c561934e089]]) -> False
17 IsNumeric(System.Action) -> False
18 Press any key to EXIT...
19 */
20 using System;
21 using System.Reflection;
22 
23 static class Program {
24     static bool IsNumeric(Type type) {
25         switch (Type.GetTypeCode(type)) {
26             case TypeCode.Byte:
27             case TypeCode.SByte:
28             case TypeCode.UInt16:
29             case TypeCode.UInt32:
30             case TypeCode.UInt64:
31             case TypeCode.Int16:
32             case TypeCode.Int32:
33             case TypeCode.Int64:
34             case TypeCode.Decimal:
35             case TypeCode.Double:
36             case TypeCode.Single:
37                 return true;
38             default:
39                 return false;
40         }
41     }
42 
43     public static void Main() {
44         Test(typeof(bool));
45         Test(typeof(string));
46         Test(typeof(char));
47         Test(typeof(byte));
48         Test(typeof(byte[]));
49         Test(typeof(DateTime));
50         Test(typeof(int));
51         Test(typeof(float));
52         Test(typeof(Decimal));
53         Test(typeof(DayOfWeek));
54         Test(typeof(Guid));
55         Test(typeof(IntPtr));
56         Test(typeof(int?));
57         Test(typeof(Action));
58         Console.Write("Press any key to EXIT...");
59         Console.ReadKey(true);
60     }
61 
62     static void Test(Type type) {
63         Console.WriteLine("IsNumeric({0}) -> {1}", type.FullName, IsNumeric(type));
64     }
65 
66 }
原文地址:https://www.cnblogs.com/Bob-wei/p/7269874.html