📜  C#|带示例的Object.GetTypeCode()方法

📅  最后修改于: 2021-05-29 15:14:33             🧑  作者: Mango

此方法用于返回当前实例的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/zh-cn/dotnet/api/system.object.gettype?view=netframework-4.7.2