當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Object.GetTypeCode()用法及代碼示例


此方法用於返回當前實例的Type。在這裏,類型表示類型聲明,即類類型,接口類型,數組類型,值類型,枚舉類型,類型參數,泛型類型定義以及打開或關閉的構造泛型類型。 System.Object類是.NET Framework類型係統中存在的所有類型的基類。本質上,此方法返回代表所有.NET Framework類型的Type對象。

.NET Framework可識別以下五類類型:

  • ,它們是從System.Object派生的。
  • 值類型,它們是從System.ValueType派生的。
  • 介麵,它們從.NET Framework 2.0開始從System.Object派生。
  • 枚舉,它們是從System.Enum派生的。
  • 代表們,它們是從System.MulticastDelegate派生的。

用法:

public Type GetType ();

返回值:此方法返回當前實例的運行時類型。

以下示例程序旨在說明Object.GetType()方法的用法:



範例1:


// C# program to demonstrate
// Object.GetType() Method
using System;
  
// Base class
public class G {
}
  
// Derived class
public class X:G {
}
  
// Driver Class
class GFG {
  
    // Main method
    public static void Main()
    {
        // Creating and initializing objects
        X obj = new X();
        G obj1 = new G();
        Object obj2 = obj;
  
        // Find the type of objects
        // using GetType() method
        Console.WriteLine("The X class object type is:" 
                                       + obj.GetType());
  
        Console.WriteLine("The G class object type is:" 
                                      + obj1.GetType());
  
        Console.WriteLine("The obj2 object type is:" 
                                   + obj2.GetType());
    }
}
輸出:
The X class object type is:X
The G class object type is:G
The obj2 object type is:X

範例2:


// C# program to demonstrate
// Object.GetType() Method
using System;
  
public class Author {
  
    public string A_Name;
    public string P_Name;
    public int n;
  
    public Author(string A_Name, 
           string P_Name, int n)
    {
        this.A_Name = A_Name;
        this.P_Name = P_Name;
        this.n = n;
    }
  
    public void Show()
    {
        Console.WriteLine("Author Name:" + A_Name);
        Console.WriteLine("Article Name:" + P_Name);
        Console.WriteLine("Article No:" + n);
    }
  
    public void type()
    {
        Console.WriteLine("Type of Author Name:" 
                              + A_Name.GetType());
  
        Console.WriteLine("Type of Article Name:" 
                               + P_Name.GetType());
  
        Console.WriteLine("Type of Article No:" 
                                  + n.GetType());
    }
}
  
// Driver Class
class GFG {
  
    // Main method
    public static void Main()
    {
        // Creating and initializing
        // the object of Author class
        Author obj = new Author("Kirti", 
                "GetType() method", 3);
  
        Console.WriteLine("Author details:");
        obj.Show();
  
        // Display the type
        obj.type();
        Console.WriteLine("Type of Author class object:" 
                                         + obj.GetType());
    }
}
輸出:
Author details:
Author Name:Kirti
Article Name:GetType() method
Article No:3
Type of Author Name:System.String
Type of Article Name:System.String
Type of Article No:System.Int32
Type of Author class object:Author

參考:https://docs.microsoft.com/en-us/dotnet/api/system.object.gettype?view=netframework-4.7.2

相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 C# | Object.GetTypeCode() Method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。