typeid (C++/CLI y C++/CX)typeid (C++/CLI and C++/CX)
Obtiene un valor que indica el tipo de un objeto.Gets a value that indicates the type of an object.
Nota
Este tema hace referencia a la versión de extensiones de componentes de C++ de typeid.This topic refers to the C++ Component Extensions version of typeid. Para obtener la versión ISO de C++ de esta palabra clave, consulte typeid (Operador).For the ISO C++ version of this keyword, see typeid Operator.
Todos los runtimesAll Runtimes
SintaxisSyntax
T::typeid
ParámetrosParameters
TT
Nombre de tipo.A type name.
Windows en tiempo de ejecuciónWindows Runtime
SintaxisSyntax
Platform::Type^ type = T::typeid;
ParámetrosParameters
TT
Nombre de tipo.A type name.
ObservacionesRemarks
En C++/CX, typeid devuelve una clase Platform::Type que se construye a partir de la información de tipo de runtime.In C++/CX, typeid returns a Platform::Type that is constructed from runtime type information.
RequisitosRequirements
Opción del compilador: /ZW
Compiler option: /ZW
Common Language RuntimeCommon Language Runtime
SintaxisSyntax
type::typeid
ParámetrosParameters
typetype
Nombre de un tipo (declarador abstracto) para el que desea usar el objeto System::Type
.The name of a type (abstract declarator) for which you want the System::Type
object.
ObservacionesRemarks
typeid
se usa para obtener Type para un tipo en tiempo de compilación.typeid
is used to get the Type for a type at compile time.
typeid
es similar a obtener System::Type
para un tipo en tiempo de ejecución mediante GetType o GetType .typeid
is similar to getting the System::Type
for a type at run time using GetType or GetType. Sin embargo, typeid
solo acepta un nombre de tipo como parámetro.However, typeid
only accepts a type name as a parameter. Si desea utilizar una instancia de un tipo para obtener su System::Type
nombre, use GetType
.If you want to use an instance of a type to get its System::Type
name, use GetType
.
typeid
debe poder evaluar un nombre de tipo (tipo) en tiempo de compilación, mientras que GetType evalúa el tipo que se va a devolver en tiempo de ejecución.typeid
must be able to evaluate a type name (type) at compile time, whereas GetType evaluates the type to return at run time.
typeid
puede tomar un nombre de tipo nativo o un alias de Common Language Runtime para el nombre de tipo nativo; para obtener más información , vea .NET Framework equivalentes a los tipos nativos de c++ (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
también funciona con tipos nativos, aunque seguirá devolviendo System::Type
.typeid
also works with native types, although it will still return a System::Type
. Para obtener una estructura de type_info, utilice el typeid
operador.To get a type_info structure, use typeid
Operator.
RequisitosRequirements
Opción del compilador: /clr
Compiler option: /clr
EjemplosExamples
En el siguiente ejemplo se compara la palabra clave typeid con el miembro 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*
En el ejemplo siguiente se muestra que se puede usar una variable de tipo System::Type para obtener los atributos de un tipo.The following sample shows that a variable of type System::Type can be used to get the attributes on a type. También se muestra que para algunos tipos, tendrá que crear una definición de tipo que se va a usar 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
Consulta tambiénSee also
Extensiones de componentes para .NET y UWPComponent Extensions for .NET and UWP