Object.GetType 方法

定義

取得目前執行個體的 Type

C#
public Type GetType ();

傳回

Type

目前執行個體的確切執行階段類型。

範例

下列程式碼範例示範如何 GetType 傳回目前實例的執行時間類型。

C#
using System;

public class MyBaseClass {
}

public class MyDerivedClass: MyBaseClass {
}

public class Test
{
   public static void Main()
   {
      MyBaseClass myBase = new MyBaseClass();
      MyDerivedClass myDerived = new MyDerivedClass();
      object o = myDerived;
      MyBaseClass b = myDerived;

      Console.WriteLine("mybase: Type is {0}", myBase.GetType());
      Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
      Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
      Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());
   }
}
// The example displays the following output:
//    mybase: Type is MyBaseClass
//    myDerived: Type is MyDerivedClass
//    object o = myDerived: Type is MyDerivedClass
//    MyBaseClass b = myDerived: Type is MyDerivedClass

備註

因為 System.Object 是 .NET 類型系統中所有型別的基類, GetType 所以方法可用來傳回 Type 代表所有 .NET 類型的物件。 .NET 可辨識下列五種類型類別:

針對兩個 物件 x ,且 y 具有相同的執行時間類型, Object.ReferenceEquals(x.GetType(),y.GetType()) 會傳 true 回 。 下列範例使用 GetType 方法搭配 ReferenceEquals 方法,判斷一個數值是否與兩個其他數值相同。

C#
int n1 = 12;
int n2 = 82;
long n3 = 12;

Console.WriteLine("n1 and n2 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n2.GetType()));
Console.WriteLine("n1 and n3 are the same type: {0}",
                  Object.ReferenceEquals(n1.GetType(), n3.GetType()));

// The example displays the following output:
//       n1 and n2 are the same type: True
//       n1 and n3 are the same type: False

注意

若要判斷物件是否為特定類型,您可以使用語言的類型比較關鍵字或建構。 例如,您可以在 TypeOf…Is Visual Basic 中使用 建構,或在 is C# 中使用 關鍵字。

方法 GetType 是由衍生自 Object 的所有型別所繼承。 這表示除了使用您自己的語言比較關鍵字之外,您還可以使用 GetType 方法來判斷特定物件的類型,如下列範例所示。

C#
object[] values = { (int) 12, (long) 10653, (byte) 12, (sbyte) -5,
                   16.3, "string" };
foreach (var value in values) {
   Type t = value.GetType();
   if (t.Equals(typeof(byte)))
      Console.WriteLine("{0} is an unsigned byte.", value);
   else if (t.Equals(typeof(sbyte)))
      Console.WriteLine("{0} is a signed byte.", value);
   else if (t.Equals(typeof(int)))
      Console.WriteLine("{0} is a 32-bit integer.", value);
   else if (t.Equals(typeof(long)))
      Console.WriteLine("{0} is a 64-bit integer.", value);
   else if (t.Equals(typeof(double)))
      Console.WriteLine("{0} is a double-precision floating point.",
                        value);
   else
      Console.WriteLine("'{0}' is another data type.", value);
}

// The example displays the following output:
//    12 is a 32-bit integer.
//    10653 is a 32-bit integer.
//    12 is an unsigned byte.
//    -5 is a signed byte.
//    16.3 is a double-precision floating point.
//    'string' is another data type.

物件 Type 會公開與目前 Object 類別相關聯的中繼資料。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱