How to check a generic type in C++/CLI?

*爱你&永不变心* 提交于 2019-12-07 07:20:48

问题


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?


回答1:


You could write:

return t->IsGenericType
    && t->GetGenericTypeDefinition() == System::Collections::Generic::IDictionary<int,int>::typeid->GetGenericTypeDefinition();


来源:https://stackoverflow.com/questions/6389149/how-to-check-a-generic-type-in-c-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!