I've read somewhere on MSDN that the equivalent to C#'s "is" keyword would be dynamic_cast, but that's not really equivalent: It doesn't work with value types or with generic parameters. For example in C# I can write:
void MyGenericFunction<T>()
{
object x = ...
if (x is T)
...;
}
If I try the "equivalent" C++/CLI:
generic<class T>
void MyGenericFunction()
{
object x = ...
if (dynamic_cast<T>(x))
...;
}
I get a compiler error "error C2682: cannot use 'dynamic_cast' to convert from 'System::Object ^' to 'T'".
The only thing I can think of is to use reflection:
if (T::typeid->IsAssignableFrom(obj->GetType()))
Is there a simpler way to do this?