Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

Object.GetType-Methode: ()

 

Ruft den Type der aktuellen Instanz ab.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public Type GetType()

Rückgabewert

Type: System.Type

Der genaue Laufzeittyp der aktuellen Instanz.

Because T:System.Object is the base class for all types in the .NET Framework type system, the M:System.Object.GetType method can be used to return T:System.Type objects that represent all .NET Framework types. The .NET Framework recognizes the following five categories of types:

  • Classes, which are derived from T:System.Object,

  • Value types, which are derived from T:System.ValueType.

  • Interfaces, which are derived from T:System.Object starting with the .NET Framework 2.0.

  • Enumerations, which are derived from T:System.Enum.

  • Delegates, which are derived from T:System.MulticastDelegate.

For two objects x and y that have identical runtime types, Object.ReferenceEquals(x.GetType(),y.GetType()) returns true. The following example uses the M:System.Object.GetType method with the M:System.Object.ReferenceEquals(System.Object,System.Object) method to determine whether one numeric value is the same type as two other numeric values.

using System;

public class Example
{
   public static void Main()
   {
      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      
System_CAPS_noteHinweis

To determine whether an object is a specific type, you can use your language's type comparison keyword or construct. For example, you can use the TypeOf…Is construct in Visual Basic or the is keyword in C#.

The M:System.Object.GetType method is inherited by all types that derive from T:System.Object. This means that, in addition to using your own language's comparison keyword, you can use the M:System.Object.GetType method to determine the type of a particular object, as the following example shows.

using System;

public class Example
{
   public static void Main()
   {
      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 32-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.

The T:System.Type object exposes the metadata associated with the class of the current T:System.Object.

The following code example demonstrates that M:System.Object.GetType returns the runtime type of the current instance.

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 

Universelle Windows-Plattform
Verfügbar seit 8
.NET Framework
Verfügbar seit 1.1
Portierbare Klassenbibliothek
Unterstützt in: portierbare .NET-Plattformen
Silverlight
Verfügbar seit 2.0
Windows Phone Silverlight
Verfügbar seit 7.0
Windows Phone
Verfügbar seit 8.1
Zurück zum Anfang
Anzeigen: