使用反射,如何使用最少的代码获得使用C#3.0/.NET 3.5实现接口的所有类型,并最大限度地减少迭代?
这就是我想要重写的内容:
foreach (Type t in this.GetType().Assembly.GetTypes())
if (t is IMyInterface)
; //do stuff
Run Code Online (Sandbox Code Playgroud) 在以下场景中调用typeof(Bar).GetInterfaces()时,该方法返回IFoo和IBar.
interface IFoo {}
interface IBar : IFoo {}
class Bar : IBar {}
有没有办法让我只能在Bar上找到直接界面(IBar)?