【发布时间】:2009-07-14 21:26:55
【问题描述】:
我有一个接口,以及一些继承自它的类。
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?
}
}
【问题讨论】:
标签: c# reflection interface