typeid(C++ 组件扩展)
获取一个指示对象类型的值。
警告
本主题引用 typeid 的 C++ 组件扩展版本。有关此关键字的ISO C++版本,请参见typeid 运算符。
所有运行时
语法
T::typeid
参数
- T
类型名称。
Windows 运行时
语法
Platform::Type^ type = T::typeid;
参数
- T
类型名称。
备注
在 C++/CX 中,typeid 返回根据运行时类型信息构造的 Platform::Type。
要求
编译器选项:/ZW
公共语言运行时
语法
type::typeid
参数
- type
您想要的 System::Type 对象的类型名称(抽象声明符)。
备注
typeid 用于在编译时获取类型的 Type。
typeid 类似于在运行时使用 GetType 或 GetType 获取类型的 System::Type。 但是,typeid 只接受作为参数的类型名称。如要使用类型实例以获取其 System::Type 名称,请使用 GetType。
typeid 必须能够在编译时评估类型名称(类型),而 GetType 将评估该类型是否在运行时返回。
typeid 可以采用本机类型名称或本机类型名称的公共语言运行时别名;有关更多信息,请参见 对应于 C++ 本机类型的 .NET Framework 类型 (C++/CLI)。
typeid 还适用于本机类型,不过它将返回 System::Type。要获取 type_info 结构,请使用 typeid 运算符。
typeid 是上述 /clr 语法的 __typeof 后继。
要求
编译器选项:/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);
}
Output
示例
以下示例说明了 System::Type 变量可用于获取在类型上的属性。对于某些类型,它还说明必须创建 typedef 以使用 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");
}
Output