获取应用程序内实现接口的所有对象
我需要获取应用程序中实现给定接口的所有对象。 我怎样才能做到这一点?
谢谢您
澄清我正在寻找不是类型的实例。
再次澄清一下,我想我需要做一些邪恶的事情,比如获取与我的 AppDomain 关联的所有线程并遍历它们的堆栈。
再次-我老板的想法...我想到使用 IOC 或 AOP 来做...
I need to get all the objects in my application that implement a given interface.
How can i achieve this?
Thank you
To clarify I'm looking for instances NOT for Types.
To clarify again, i guess ill need to do something evil like get all the threads associated with my AppDomain and walk their stacks.
Again- my boss's idea... i thought of doing using IOC or AOP....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这将搜索所有加载的程序集。
This will search all the loaded assemblies.
这是不可能的。
要查找对象,您需要对其进行引用。如果您没有对它的引用,但想“找到”它,这是不可能的。
This cannot be done.
To find an object, you need a reference to it. If you do not have a reference to it, but want to "find" it, this is not possible.
一种方法是使用 MEF,它的性能可能比使用标准反射更好。
http://mef.codeplex.com/
您基本上可以向所有接口实现添加一个属性,然后 MEF 就可以扫描您的程序集(以及您告诉它扫描的任何文件夹中的任何其他程序集)以获取实现。
One way to do it would be to use MEF and it would probably perform better than using standard reflection.
http://mef.codeplex.com/
You basically add an attribute to all your interface implementations and MEF can then scan your assemblies (and any others in any folders you tell it to scan) for the implementations.
因此,您想要查找实现特定接口的所有实例,而不是类,而是实际的对象。您需要以某种方式枚举对象的实例。我不知道有什么方法可以做到这一点。
但是,如果您可以为您拥有的所有相关对象(例如您自己的 Object 类)创建某种共同祖先,那么您可以添加由以下方法创建的所有实例的静态枚举:在构造函数中注册它们。然后,您可以简单地运行它们并检查每个实例以查看它是否实现了该接口。当然,这种方法的问题是,即使您使用完这些实例,它们也永远不会被垃圾回收,因为它们始终有一个指向它们的引用。
这使我得出结论,可能没有一种“内置”方式来枚举类或接口的实例,甚至枚举内存中的所有实例,因为拥有任何此类枚举对象意味着这些实例可以即使您不再引用它们,也永远不会被垃圾收集,因为它们仍然可以被“引用”。这个想法与托管代码的整个要点相矛盾......因此,除非您找到自己的方法来枚举实例,否则我认为您无法做到这一点。
希望有帮助:-)
So you want to find all instances that implement a specific interface - not the classes, the actual objects. You need to somehow enumerate over instances of objects. I am not aware of a way to do that.
If you could, however, create some sort of common ancestor for all the relevant objects that you have (something like your own Object class) then you can add a static enumeration of all the instances that were created by registering them in the constructor. You can then simply run over them and check each instance to see if it implements the interface. The problem with this approach is, of course, that these instances will never be garbage collected, even when you are done using them because they always have a reference pointing to them.
This leads me to the concolusion that there probably isn't a "built in" way to enumerate over instances of a class or interface, or even to enumerate over all the instances in the memory because having any such enumeration object means that these instances can never be garbage collected even when you no longer have a reference to them, because they can still be "referenced". This idea condradicts the whole point in managed code... So unless you find your own way to enumerate the instances, I don't think you will be able to do that.
hope that helps :-)