Object.GetType 方法

获取当前实例的 Type

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Function GetType As Type
用法
Dim instance As Object
Dim returnValue As Type

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

返回值

Type 实例,表示当前实例的确切运行时类型。

备注

对于具有相同运行时类型的两个对象 x 和 y,Object.ReferenceEquals(x.GetType(),y.GetType()) 返回 true

Type 对象公开与当前 Object 的类关联的元数据。

示例

下面的代码示例说明 GetType 返回当前实例的运行时类型。

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 

*/

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Object 类
Object 成员
System 命名空间
Type