删除类的所有实例的最佳方法? [英] Best way to remove all instances of a class ?

查看:71
本文介绍了删除类的所有实例的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您是否可以给我一些提示,以解决此问题的最佳方法..

我有一个自定义控件(类)(实际上是一个按钮),在运行时我对其进行了多次初始化. 当前要对按钮应用更改,我需要重新启动应用程序-但现在我已经厌倦了重新启动应用程序-因此,我希望删除该类的所有实例(包括在创建过程中初始化的句柄),然后再删除使用新设置清除病房后的新实例

这样做的最佳方法是什么?
我应该在哈希表或集合中保留对控件的引用,然后从那里清除它们吗?

我还以为有一个函数可以返回一个类的所有初始化实例-但是我找不到指向该方向的任何东西

I was wondering if you could give me a hint on what''s the best way of approaching this ..

I have a custom control(class) (It''s actually a button) that I initialize multiple times during run time..
Currently to apply changes to the buttons, I need to restart the application - but now I''m getting tired of doing so - therefore I would prefer to remove all Instances of the class (including the handles that I initialized during creation) and then clear new instances after wards with the new settings

What would be the best approach to do so ??
Should I just hold a ref to the control in a hash table or collection and then just clear them from there ?

I also thought there was a function that returns all initialized instances of a class - but I could not find anything pointing in that direction anymore

Any Imput would be appropriated

推荐答案

基本上,最好的方法是将所有实例收集在某个容器中.请稍等片刻,不要那么快.您的想法很可能是滥用垃圾回收的体系结构.但也许不是.整理一下吧.

您不能只是删除实例",因为托管应用程序中没有这样的东西.您只需要删除对实例的引用,它们又被存储在某个容器中.因此,在最简单的情况下,您只需要清除容器即可.

希望您已经了解了这一点,但是需要澄清一下.如果您的对象丢失了reachability,则堆上的所有对象都会被垃圾回收器(GC)破坏并从内存中删除.可到达性根本不是一个简单的概念.例如,如果三个对象在循环中互相引用(A-> B-> C-> A),但是对它们中的任何一个都没有更多引用,则无论如何都将对其进行垃圾回收.

我可以想象你的照片更加复杂.有许多不同的容器,即嵌套容器,每个容器都包含不同类型的对象(多态与否).通常,您应该递归地遍历所有容器并清除它们.这是最好的方法.

让我们想象一下,无论出于何种原因,这对您来说都是困难的.然后,您可以收集所有要清除的孩子与父母之间的关系,并将其放在一个单独的容器中.存储不是一件容易的事.您将需要存储一些数据和引用,以将对象从其父容器中删除.也许应该只是容器的集合.这取决于您不知道的设计.我的事是给你正确的主意.

这里的问题是这样的:这个额外的集合本身将保存对象,因此将使它们无法被收集.有两种方法可以解决此问题. 1)完成工作后,您将需要自己清理这个额外的收藏集; 2)您可以保存弱引用",而不是真实"引用,而弱引用"可用于进行引用,这将允许垃圾回收.

使用类System.WeakReference解决了弱引用问题,请参阅 http://msdn. microsoft.com/en-us/library/system.weakreference.aspx [ Paulo Zemec 有趣的文章:
创建弱列表 [弱引用,GCHandles和WeakArrays [创建弱事件 [
Basically, the best approach is collecting all of the instances in some container. Wait a minute, not so fast. There is a possibility that your thinking is to abuse the garbage-collected architecture. But maybe not. Let''s sort it out.

You cannot just "remove instance", as there is no such thing in managed application. You only need to remove the referenced to the instances, and they are, again, are stored in some container. So, in the simplest case, you simply need to clear the container.

I hope you already understand this, but it need some clarification. Any object on heap will be destructed and removed from memory by the Garbage Collector (GC) if your object lost its reachability. Reachability is not a simple concept at all. For example, if three objects are referencing each other in cycle (A -> B -> C -> A), but there no more references to either of them, they will be garbage-collected anyway.

I can imagine that your picture is more complex. There are many different containers, nesting containers, each containing objects of different types, polymorphous or not. Usually, you should recursively traverse all your containers and clear them. This is the best way.

Let''s imaging that this is difficult for you, by whatever reason. Then you could collect all the child-parent relationship to be cleared in some separate container. The storing is not trivial; you will need to store some data and references needed to remove an object from its parent container. Maybe, it should be just the collection of containers. It depends on your design which I don''t know. My thing is to give you the right idea.

The problem here is this: this extra collection itself will hold objects and thus will keep them from collection. There are two ways to work around this problem. 1) You will need to clean this extra collection itself when the work is done; 2) you can hold not "real" references, but "weak references", which can be used for referencing by will allow garbage collection.

The problem of weak referencing is resolved using the class System.WeakReference, please see http://msdn.microsoft.com/en-us/library/system.weakreference.aspx[^].

I would also recommend to read the interesting articles by CodeProject author Paulo Zemec:
Creating a Weak List[^],
WeakReferences, GCHandles, and WeakArrays[^],
Creating a weak event[^].

—SA


这篇关于删除类的所有实例的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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