创建一个从IBlahblah继承的所有类的集合 [英] Creating a collection of all classes that inherit from IBlahblah

查看:108
本文介绍了创建一个从IBlahblah继承的所有类的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用反射(我猜),是否有可能创建一个将返回名为IBlahblah接口继承所有对象的集合?



<$的方法p $ p> public接口IBlahblah;


解决方案

假设你有一个组件(或组件列表)看看,你可以得到一个集合的类型而实现一个接口的:

  VAR无聊症= assembly.GetTypes()
。凡(T => typeof运算(IBlahblah).IsAssignableFrom(T));

您不能得到活物,虽然实现接口的集合 - 至少在没有使用调试/分析API或类似的东西。


Using reflection (i'm guessing?), is it possible to create a method that will return a collection of all objects that inherit from an interface named IBlahblah?

public interface IBlahblah;

解决方案

Assuming you have an assembly (or a list of assemblies) to look in, you can get a collection of types which implement an interface:

var blahs = assembly.GetTypes()
                    .Where(t => typeof(IBlahblah).IsAssignableFrom(t));

You can't get a collection of "live objects" implementing the interface though - at least not without using the debugging/profiling API or something similar.

这篇关于创建一个从IBlahblah继承的所有类的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆