Object.GetType Metoda

Definicja

Type Pobiera wartość bieżącego wystąpienia.

C#
public Type GetType ();

Zwraca

Type

Dokładny typ środowiska uruchomieniowego bieżącego wystąpienia.

Przykłady

Poniższy przykład kodu pokazuje, że GetType zwraca typ środowiska uruchomieniowego bieżącego wystąpienia.

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

Uwagi

Ponieważ System.Object jest klasą bazową dla wszystkich typów w systemie typów platformy .NET, GetType metoda może służyć do zwracania Type obiektów reprezentujących wszystkie typy platformy .NET. Platforma .NET rozpoznaje następujące pięć kategorii typów:

W przypadku dwóch obiektów x , y które mają identyczne typy środowiska uruchomieniowego, Object.ReferenceEquals(x.GetType(),y.GetType()) zwraca wartość true. W poniższym przykładzie użyto GetType metody z ReferenceEquals metodą , aby określić, czy jedna wartość liczbowa jest tym samym typem co dwie inne wartości liczbowe.

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

Uwaga

Aby określić, czy obiekt jest określonym typem, możesz użyć słowa kluczowego porównania typów języka lub konstrukcji. Można na przykład użyć TypeOf…Is konstrukcji w Visual Basic lub słowa kluczowego is w języku C#.

Metoda GetType jest dziedziczona przez wszystkie typy pochodzące z Objectklasy . Oznacza to, że oprócz używania słowa kluczowego porównania języka można użyć GetType metody w celu określenia typu określonego obiektu, jak pokazano w poniższym przykładzie.

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.

Obiekt Type uwidacznia metadane skojarzone z klasą bieżącego Objectobiektu .

Dotyczy

Produkt Wersje
.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

Zobacz też