【发布时间】:2009-04-16 05:34:38
【问题描述】:
我正在尝试编写验证以检查 Object 实例是否可以转换为变量 Type。对于他们需要提供的对象类型,我有一个 Type 实例。但类型可能会有所不同。这基本上是我想做的。
Object obj = new object();
Type typ = typeof(string); //just a sample, really typ is a variable
if(obj is typ) //this is wrong "is" does not work like this
{
//do something
}
类型对象本身具有 IsSubClassOf 和 IsInstanceOfType 方法。但我真正想检查的是 obj 是 typ 的实例还是派生自 typ 的任何类。
似乎是一个简单的问题,但我似乎无法弄清楚。
【问题讨论】: