📜  C#| Type.GetTypeCode()方法

📅  最后修改于: 2021-05-29 22:01:44             🧑  作者: Mango

Type.GetTypeCode()方法用于获取指定Type的基础类型代码。

下面的程序说明了Type.GetTypeCode()方法的用法:

范例1:

// C# program to demonstrate the
// Type.GetTypeCode() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // creating and initializing object Type
        Type type = typeof(int);
  
        // Getting the TypeCode
        TypeCode code = Type.GetTypeCode(type);
  
        // Display the Result
        Console.WriteLine("TypeCode is {0}", code);
    }
}
输出:
TypeCode is Int32

范例2:

// C# program to demonstrate the
// Type.GetTypeCode() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Calling get() Method
        get(typeof(int));
        get(typeof(decimal));
        get(typeof(double));
        get(typeof(short));
        get(typeof(string));
    }
  
    // defining get Method
    public static void get(Type type)
    {
  
        // Getting Typecode
        TypeCode code = Type.GetTypeCode(type);
  
        // Display the Result
        Console.WriteLine("TypeCode of {1} is {0}", code, type);
    }
}
输出:
TypeCode of System.Int32 is Int32
TypeCode of System.Decimal is Decimal
TypeCode of System.Double is Double
TypeCode of System.Int16 is Int16
TypeCode of System.String is String

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.type.gettypecode?view=netframework-4.8