在 C++/CLI 代码中,我需要检查该类型是否是特定的泛型类型。在 C# 中,它将是:
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( Type^ t ) {
return 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) 的限制还是我不知道“正确”的系统税?
请您参考如下方法:
你可以写:
return t->IsGenericType
&& t->GetGenericTypeDefinition() == System::Collections::Generic::IDictionary<int,int>::typeid->GetGenericTypeDefinition();


