如何在C ++ / CLI中检查对象的类型? [英] How to check an object's type in C++/CLI?
问题描述
有没有简单的方法来检查对象的类型?我需要一些沿着以下行:
Is there a simple way to check the type of an object? I need something along the following lines:
MyObject^ mo = gcnew MyObject();
Object^ o = mo;
if( o->GetType() == MyObject )
{
// Do somethine with the object
}
else
{
// Try something else
}
m使用嵌套的try-catch块寻找 System :: InvalidCastException
s感觉丑陋但工作。我将尝试并配置类似上面的代码,看看它是否更快/更慢/可读,但无法解决语法甚至尝试。
At the moment I'm using nested try-catch blocks looking for System::InvalidCastException
s which feels ugly but works. I was going to try and profile something like the code above to see if it's any faster/slower/readable but can't work out the syntax to even try.
在case任何人的想法,这来自于有一个单一的队列进入一个线程提供数据工作。
In case anyone's wondering, this comes from having a single queue entering a thread which supplied data to work on. Occasionally I want to change settings and passing them in via the data queue is a simple way of doing so.
推荐答案
有时候,我想通过数据队列更改设置和传递它们。 在C ++ / CLI中使用类似于
typeof(MyObject)
的方法在C#中使用下面的代码无耻地复制你的问题和修改...
You can use MyObject::typeid
in C++/CLI the same way as typeof(MyObject)
is used in C#. Code below shamelessly copied from your question and modified ...
MyObject^ mo = gcnew MyObject();
Object^ o = mo;
if( o->GetType() == MyObject::typeid )
{
// Do somethine with the object
}
else
{
// Try something else
}
这篇关于如何在C ++ / CLI中检查对象的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!