AC.*_*AC. 4 generics c++-cli typeid
在C++/CLI代码中,我需要检查类型是否是特定的泛型类型.在C#中它将是:
public static class type_helper {
public static bool is_dict( Type t ) {
return t.IsGenericType
&& t.GetGenericTypeDefinition() == typeof(IDictionary<,>);
}
}
Run Code Online (Sandbox Code Playgroud)
但是在cpp ++\cli中它的工作方式不同,编译器会显示语法错误:
class type_helper {
public:
static bool is_dict( Type^ t ) {
return t->IsGenericType && t->GetGenericTypeDefinition()
== System::Collections::Generic::IDictionary<,>::typeid;
}
};
Run Code Online (Sandbox Code Playgroud)
我找到的最好方法是比较这样的字符串:
class type_helper {
public:
static bool is_dict( Type^ t ) {
return t->IsGenericType
&& t->GetGenericTypeDefinition()->Name == "IDictionary`2";
}
};
Run Code Online (Sandbox Code Playgroud)
有人知道更好的方法吗?
PS:c ++\cli中的typeof(typeid)是否有限制,或者我不知道"正确"的systax?
你可以写:
return t->IsGenericType
&& t->GetGenericTypeDefinition() == System::Collections::Generic::IDictionary<int,int>::typeid->GetGenericTypeDefinition();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1313 次 |
| 最近记录: |