typeid(C++ 组件扩展)

获取一个指示对象类型的值。

所有运行时

kwd9abya.collapse_all(zh-cn,VS.110).gif语法

T::typeid

kwd9abya.collapse_all(zh-cn,VS.110).gif参数

  • T
    类型名称。

运行时的窗口

kwd9abya.collapse_all(zh-cn,VS.110).gif语法

Platform::Type^ = T::typeid;

kwd9abya.collapse_all(zh-cn,VS.110).gif参数

  • T
    类型名称。

kwd9abya.collapse_all(zh-cn,VS.110).gif备注

在 C++/CX, typeid 返回从运行时类型信息构造的 Platform::Type^。

kwd9abya.collapse_all(zh-cn,VS.110).gif要求

编译器选项: /ZW

公共语言运行时

语法

type::typeid

参数

  • type
    (抽象声明) 的所需 System::Type 对象的名称类型。

备注

typeid 用于获取类型的 Type 在编译时。

typeid 类似于获取类型的 System::Type 在运行时使用 GetTypeGetType。 但是, typeid 只接受一个类型名称作为参数。 如果要使用类型获取的实例其 System::Type 名称,请使用 GetType。

typeid 必须能够计算类型名称 (类型) 在编译时,,而 GetType 计算该类型返回在运行时。

typeid 可以采用本机类型名称或公共语言本机类型的名称运行时别名;请参见 对应于 C++ 本机类型的 .NET Framework 类型 (C++/CLI) 有关更多信息。

typeid 还与本机类型一起使用,不过,它将返回 System::Type。 获取 type_info 结构,使用 typeid运算符

typeid 是 sucessor 设置为在上述 /clr 语法的 __typeof

kwd9abya.collapse_all(zh-cn,VS.110).gif要求

编译器选项: /clr

kwd9abya.collapse_all(zh-cn,VS.110).gif示例

示例

下面的示例与 GetType () 成员比较 typeid 关键字。

// 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

  
  
  
  
  
  
  
  

请参见

概念

适用于运行时平台的组件扩展