次の方法で共有


Object.GetType メソッド

現在のインスタンスの Type を取得します。

Public Function GetType() As Type
[C#]
public Type GetType();
[C++]
public: Type* GetType();
[JScript]
public function GetType() : Type;

戻り値

現在のインスタンスの正確なランタイム型を表す Type インスタンス。

解説

2 つのオブジェクト xy のランタイム型が同一の場合には、 Object.ReferenceEquals(x.GetType(),y.GetType())true を返します。

Type オブジェクトは、現在の Object に関連付けられているメタデータを公開します。

使用例

[C#, C++, JScript] 現在のインスタンスのランタイム型を返す 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 

*/

[C++] 
#using <mscorlib.dll>
using namespace System;

public __gc class MyBaseClass: public Object {
};

public __gc class MyDerivedClass: public MyBaseClass {
};

int main() {
   MyBaseClass* myBase = new MyBaseClass();
   MyDerivedClass* myDerived = new MyDerivedClass();
   Object* o = myDerived;
   MyBaseClass* b = myDerived;

   Console::WriteLine(S"mybase: Type is {0}", myBase->GetType());
   Console::WriteLine(S"myDerived: Type is {0}", myDerived->GetType());
   Console::WriteLine(S"object o = myDerived: Type is {0}", o->GetType());
   Console::WriteLine(S"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 

*/

[JScript] 
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 

*/

[Visual Basic] Visual Basic のサンプルはありません。C#、C++、および JScript のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Object クラス | Object メンバ | System 名前空間 | Type