Object.GetType Yöntem

Tanım

Type Geçerli örneğini alır.

C#
public Type GetType ();

Döndürülenler

Type

Geçerli örneğin tam çalışma zamanı türü.

Örnekler

Aşağıdaki kod örneği, geçerli örneğin çalışma zamanı türünü döndürdüğünü GetType gösterir.

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

Açıklamalar

.NET tür sistemindeki tüm türler için temel sınıf olduğundan System.Object , GetType yöntemi tüm .NET türlerini temsil eden nesneleri döndürmek Type için kullanılabilir. .NET aşağıdaki beş tür kategorisini tanır:

Aynı çalışma zamanı türlerine sahip iki nesne x için döndürür Object.ReferenceEquals(x.GetType(),y.GetType()) true.y Aşağıdaki örnek, bir sayısal değerin GetType ReferenceEquals diğer iki sayısal değerle aynı türde olup olmadığını belirlemek için yöntemiyle yöntemini kullanır.

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

Not

Bir nesnenin belirli bir tür olup olmadığını belirlemek için dilinizin tür karşılaştırma anahtar sözcüğünü veya yapısını kullanabilirsiniz. Örneğin, Visual Basic içindeki yapısını veya C# dilinde anahtar sözcüğünü is kullanabilirsinizTypeOf…Is.

GetType yöntemi, 'den Objecttüretilen tüm türler tarafından devralınır. Bu, kendi dilinizin karşılaştırma anahtar sözcüğünü kullanmaya ek olarak, aşağıdaki örnekte gösterildiği gibi belirli bir nesnenin türünü belirlemek için yöntemini kullanabileceğiniz GetType anlamına gelir.

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 nesnesi, geçerli Objectöğesinin sınıfıyla ilişkili meta verileri kullanıma sunar.

Şunlara uygulanır

Ürün Sürümler
.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

Ayrıca bkz.