当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。