英語で読む

次の方法で共有


Object.GetType メソッド

定義

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

C#
public Type GetType ();

戻り値

Type

現在のインスタンスの正確なランタイム型。

次のコード例は、現在のインスタンスのランタイム型を返す GetType 方法を示しています。

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

注釈

.NET 型システムのすべての型の基本クラスであるため System.Object 、このメソッドを GetType 使用して、すべての .NET 型を表すオブジェクトを返 Type すことができます。 .NET では、次の 5 つの種類のカテゴリが認識されます。

同じランタイム型を持つ 2 つのオブジェクトx``yの場合は、Object.ReferenceEquals(x.GetType(),y.GetType())返しますtrue。 次の例では、メソッドと共に GetType メソッドを ReferenceEquals 使用して、1 つの数値が他の 2 つの数値と同じ型であるかどうかを判断します。

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

注意

オブジェクトが特定の型であるかどうかを判断するには、言語の型比較キーワードまたはコンストラクトを使用できます。 たとえば、Visual BasicのTypeOf…Isコンストラクトや C# のキーワードをis使用できます。

この GetType メソッドは、派生元のすべての型 Objectによって継承されます。 つまり、次の例に示すように、独自の言語の比較キーワードを使用するだけでなく、メソッドを使用 GetType して特定のオブジェクトの型を決定できます。

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 、現在 Objectのクラスに関連付けられているメタデータを公開します。

適用対象

製品 バージョン
.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

こちらもご覧ください