¿Cómo verificar un tipo genérico en C ++ / CLI?
Pregunta
En el código C ++ / CLI, necesito verificar si el tipo es un tipo genérico específico.En C # sería:
public static class type_helper {
public static bool is_dict( Type t ) {
return t.IsGenericType
&& t.GetGenericTypeDefinition() == typeof(IDictionary<,>);
}
}
pero en cpp ++ \ cli no funciona de la misma manera, el compilador muestra el error de sintaxis:
class type_helper {
public:
static bool is_dict( Type^ t ) {
return t->IsGenericType && t->GetGenericTypeDefinition()
== System::Collections::Generic::IDictionary<,>::typeid;
}
};
La mejor forma que encuentro es comparar cadenas como esta:
class type_helper {
public:
static bool is_dict( Type^ t ) {
return t->IsGenericType
&& t->GetGenericTypeDefinition()->Name == "IDictionary`2";
}
};
¿Alguien conoce la mejor manera?
PD: ¿Es una limitación de typeof (typeid) en c ++ \ cli o no sé la systax "correcta"?
Solución
Podría escribir:
return t->IsGenericType
&& t->GetGenericTypeDefinition() == System::Collections::Generic::IDictionary<int,int>::typeid->GetGenericTypeDefinition();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow