typeid (C++/CLI および C++/CX)

オブジェクトの型を示す値を取得します。

Note

このトピックでは、typeid の C++ コンポーネント拡張バージョンを示します。 このキーワードの ISO C++ バージョンについては、「typeid 演算子」を参照してください。

すべてのランタイム

構文

T::typeid

パラメーター

T
型の名前。

Windows ランタイム

構文

Platform::Type^ type = T::typeid;

パラメーター

T
型の名前。

解説

C++/CX では、typeid は、ランタイムの型情報から構築された Platform::Type を返します。

必要条件

コンパイラ オプション: /ZW

共通言語ランタイム

構文

System::Type^ type = T::typeid;

パラメーター

type
System::Type オブジェクトに適用する型 (抽象宣言子) の名前。

解説

typeid を使用して、コンパイル時に型の Type を取得します。

typeid の機能は、実行時に GetType または GetType を使用して型の System::Type を取得することに似ています。 ただし、typeid には型名をパラメーターとしてのみ指定できます。 型のインスタンスを使用して、その System::Type 名を取得するには、GetType を使用します。

typeid はコンパイル時に型名 (型) を評価できる必要がありますが、GetType は実行時に返す型を評価します。

typeid には、ネイティブ型の名前またはネイティブ型の名前の共通言語ランタイム エイリアスを指定できます。詳細については、「C++ ネイティブ型と等価な .NET Framework ネイティブ型」を参照してください。

typeid ではネイティブ型も扱えますが、その場合でも System::Type を返します。 type_info 構造体を取得するには、typeid 演算子を使用します。

必要条件

コンパイラ オプション: /clr

次の例では、typeid キーワードと GetType() メンバーを比較します。

// keyword__typeid.cpp
// compile with: /clr
using namespace System;

ref struct G {
   int i;
};

int main() {
   G ^ pG = gcnew G;
   Type ^ pType = pG->GetType();
   Type ^ pType2 = G::typeid;

   if (pType == pType2)
      Console::WriteLine("typeid and GetType returned the same System::Type");
   Console::WriteLine(G::typeid);

   typedef float* FloatPtr;
   Console::WriteLine(FloatPtr::typeid);
}
typeid and GetType returned the same System::Type
G

System.Single*

次の例では、System::Type 型の変数を使用して型の属性を取得できることを示しています。 この例を見るとわかるように、一部の型では、typeid を使用するために typedef を作成する必要があります。

// keyword__typeid_2.cpp
// compile with: /clr
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;

typedef int ^ handle_to_int;
typedef int * pointer_to_int;

public ref class MyClass {};

class MyClass2 {};

[attribute(AttributeTargets::All)]
ref class AtClass {
public:
   AtClass(Type ^) {
      Console::WriteLine("in AtClass Type ^ constructor");
   }
};

[attribute(AttributeTargets::All)]
ref class AtClass2 {
public:
   AtClass2() {
      Console::WriteLine("in AtClass2 constructor");
   }
};

// Apply the AtClass and AtClass2 attributes to class B
[AtClass(MyClass::typeid), AtClass2]
[AttributeUsage(AttributeTargets::All)]
ref class B : Attribute {};

int main() {
   Type ^ MyType = B::typeid;

   Console::WriteLine(MyType->IsClass);

   array<Object^>^ MyArray = MyType -> GetCustomAttributes(true);
   for (int i = 0 ; i < MyArray->Length ; i++ )
      Console::WriteLine(MyArray[i]);

   if (int::typeid != pointer_to_int::typeid)
      Console::WriteLine("int::typeid != pointer_to_int::typeid, as expected");

   if (int::typeid == handle_to_int::typeid)
      Console::WriteLine("int::typeid == handle_to_int::typeid, as expected");
}
True

in AtClass2 constructor

in AtClass Type ^ constructor

AtClass2

System.AttributeUsageAttribute

AtClass

int::typeid != pointer_to_int::typeid, as expected

int::typeid == handle_to_int::typeid, as expected

関連項目

.NET および UWP でのコンポーネント拡張