我有一个接口和一些继承它的类。从接口类型检测类
public interface IFoo {}
public class Bar : IFoo {}
public class Baz : IFoo {}
如果我得到它实现IFoo
的类型,我怎么能决定是否型将是一个Bar
或Baz
(而不实际创建的对象)?
// Get all types in assembly.
Type[] theTypes = asm.GetTypes();
// See if a type implement IFoo.
for (int i = 0; i < theTypes.Length; i++)
{
Type t = theTypes[i].GetInterface("IFoo");
if (t != null)
{
// TODO: is t a Bar or a Baz?
}
}
好了,所以我用IsSubclassOf [I]测试theTypes ......是正确的比较方法?简单的'=='似乎不起作用。 – Nick 2009-07-14 21:32:05