typeid (C++/CLI および C++/CX)typeid (C++/CLI and C++/CX)
オブジェクトの型を示す値を取得します。Gets a value that indicates the type of an object.
注意
このトピックでは、typeid の C++ コンポーネント拡張バージョンを示します。This topic refers to the C++ Component Extensions version of typeid. このキーワードの ISO C++ バージョンについては、「typeid 演算子」を参照してください。For the ISO C++ version of this keyword, see typeid Operator.
すべてのランタイムAll Runtimes
構文Syntax
T::typeid
パラメーターParameters
TT
型の名前。A type name.
Windows ランタイムWindows Runtime
構文Syntax
Platform::Type^ type = T::typeid;
パラメーターParameters
TT
型の名前。A type name.
解説Remarks
C++/CX では、typeid は、ランタイムの型情報から構築された Platform::Type を返します。In C++/CX, typeid returns a Platform::Type that is constructed from runtime type information.
要件Requirements
コンパイラ オプション: /ZW
Compiler option: /ZW
共通言語ランタイムCommon Language Runtime
構文Syntax
type::typeid
パラメーターParameters
typetype
System::Type
オブジェクトに適用する型 (抽象宣言子) の名前。The name of a type (abstract declarator) for which you want the System::Type
object.
解説Remarks
typeid
は、 Type コンパイル時に型のを取得するために使用されます。typeid
is used to get the Type for a type at compile time.
typeid
は、 System::Type
実行時にまたはを使用して型のを取得するのと似てい GetType GetType ます。typeid
is similar to getting the System::Type
for a type at run time using GetType or GetType. ただし、では、 typeid
パラメーターとして型名のみが受け入れられます。However, typeid
only accepts a type name as a parameter. 型のインスタンスを使用して名前を取得する場合は System::Type
、を使用し GetType
ます。If you want to use an instance of a type to get its System::Type
name, use GetType
.
typeid
はコンパイル時に型名 (型) を評価できる必要がありますが、GetType は実行時に返される型を評価します。typeid
must be able to evaluate a type name (type) at compile time, whereas GetType evaluates the type to return at run time.
typeid
ネイティブ型名またはネイティブ型名の共通言語ランタイムエイリアスを受け取ることができます。詳細については、「 C++ ネイティブ型に相当する .NET Framework (c++/cli)」を 参照してください。typeid
can take a native type name or common language runtime alias for the native type name; see .NET Framework Equivalents to C++ Native Types (C++/CLI) for more information.
typeid
はネイティブ型とも連携しますが、は引き続きを返し System::Type
ます。typeid
also works with native types, although it will still return a System::Type
. Type_info 構造体を取得するには、 typeid
演算子を使用します。To get a type_info structure, use typeid
Operator.
要件Requirements
コンパイラ オプション: /clr
Compiler option: /clr
例Examples
次の例では、typeid キーワードと GetType()
メンバーを比較します。The following example compares the typeid keyword to the GetType()
member.
// 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 型の変数を使用して型の属性を取得できることを示しています。The following sample shows that a variable of type System::Type can be used to get the attributes on a type. また、一部の型では、使用する typedef を作成する必要があることも示してい typeid
ます。It also shows that for some types, you will have to create a typedef to use typeid
.
// 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
関連項目See also
.NET および UWP 用のコンポーネントの拡張機能Component Extensions for .NET and UWP