Freigeben über


Object.GetType-Methode

Ruft den Type der aktuellen Instanz ab.

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

Syntax

'Declaration
Public Function GetType As Type
'Usage
Dim instance As Object
Dim returnValue As Type

returnValue = instance.GetType
public Type GetType ()
public:
Type^ GetType ()
public Type GetType ()
public function GetType () : Type

Rückgabewert

Die Type-Instanz, die den exakten Laufzeittyp der aktuellen Instanz darstellt.

Hinweise

Für zwei Objekte x und y mit identischen Laufzeittypen gibt Object.ReferenceEquals(x.GetType(),y.GetType())true zurück.

Das Type-Objekt macht die der Klasse des aktuellen Object zugeordneten Metadaten verfügbar.

Beispiel

Das folgende Beispiel veranschaulicht, dass GetType den Laufzeittyp der aktuellen Instanz zurückgibt.

using System;

public class MyBaseClass: Object {
}

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());
   }
}


/*

This code produces the following output.

mybase: Type is MyBaseClass
myDerived: Type is MyDerivedClass
object o = myDerived: Type is MyDerivedClass
MyBaseClass b = myDerived: Type is MyDerivedClass 

*/
using namespace System;
public ref class MyBaseClass: public Object{};

public ref class MyDerivedClass: public MyBaseClass{};

int main()
{
   MyBaseClass^ myBase = gcnew MyBaseClass;
   MyDerivedClass^ myDerived = gcnew 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() );
}

/*

This code produces the following output.

mybase: Type is MyBaseClass
myDerived: Type is MyDerivedClass
object o = myDerived: Type is MyDerivedClass
MyBaseClass b = myDerived: Type is MyDerivedClass 

*/
import System.*;

public class MyBaseClass extends Object
{
} //MyBaseClass

public class MyDerivedClass extends MyBaseClass
{
} //MyDerivedClass

public class Test
{
    public static void main(String[] args)
    {
        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());
    } //main
} //Test

/*

This code produces the following output.

mybase: Type is MyBaseClass
myDerived: Type is MyDerivedClass
object o = myDerived: Type is MyDerivedClass
MyBaseClass b = myDerived: Type is MyDerivedClass 

*/
import System

public class MyBaseClass extends Object {
}

public class MyDerivedClass extends MyBaseClass {
}

public class Test {

   public static function Main() {
      var myBase : MyBaseClass = new MyBaseClass();
      var myDerived : MyDerivedClass = new MyDerivedClass();
      var o = myDerived;
      var b : MyBaseClass = 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());
   }
}

Test.Main();

/*

This code produces the following output.

mybase: Type is MyBaseClass
myDerived: Type is MyDerivedClass
object o = myDerived: Type is MyDerivedClass
MyBaseClass b = myDerived: Type is MyDerivedClass 

*/

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Object-Klasse
Object-Member
System-Namespace
Type