本文介绍了如何检查C ++ / CLI中的泛型类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++ / CLI代码中,我需要检查类型是否是特定的泛型类型。在C#中它将是:pre $
public static class type_helper {
public static bool is_dict(Type t){
return t.IsGenericType
&& t.GetGenericTypeDefinition()== typeof(IDictionary<,>);
}
}
但在cpp ++ \cli中不起作用编译器显示语法错误:
class type_helper {
public:
static bool is_dict类型^ t){
返回t-> IsGenericType&& t-> GetGenericTypeDefinition()
== System :: Collections :: Generic :: IDictionary<,> typeid;
}
};
我发现的最佳方式是比较这样的字符串:
class type_helper {
public:
static bool is_dict(Type ^ t){
return t-> IsGenericType
&安培;&安培; t-> GetGenericTypeDefinition() - > Name ==IDictionary`2;
}
};
有人知道更好的方法吗?
PS:
在c ++ \cli中是typeof(typeid)的限制还是我不知道正确的systax?
解决方案你可以这样写:
return t-> IsGenericType
&& > GetGenericTypeDefinition()== System :: Collections :: Generic :: IDictionary< int,int> :: typeid-> GetGenericTypeDefinition();
In C++/CLI code I need to check if the type is a specific generic type. In C# it would be:
public static class type_helper {
public static bool is_dict( Type t ) {
return t.IsGenericType
&& t.GetGenericTypeDefinition() == typeof(IDictionary<,>);
}
}
but in cpp++\cli it does not work the same way, compiler shows the syntax error:
class type_helper {
public:
static bool is_dict( Type^ t ) {
return t->IsGenericType && t->GetGenericTypeDefinition()
== System::Collections::Generic::IDictionary<,>::typeid;
}
};
The best way I find is compare strings like this:
class type_helper {
public:
static bool is_dict( Type^ t ) {
return t->IsGenericType
&& t->GetGenericTypeDefinition()->Name == "IDictionary`2";
}
};
Does anybody know the better way?
PS:Is it limitation of typeof (typeid) in c++\cli or I do not know "correct" systax?
解决方案
You could write:
return t->IsGenericType
&& t->GetGenericTypeDefinition() == System::Collections::Generic::IDictionary<int,int>::typeid->GetGenericTypeDefinition();
这篇关于如何检查C ++ / CLI中的泛型类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-14 07:17