清除控制不处置这些 - 什么是风险? [英] Clear controls does not dispose them - what is the risk?
问题描述
有多个线程(一个,的b ,<一个href="http://stackoverflow.com/questions/1378668/clearing-controls-from-flowlayoutpanel-not-calling-destructors">c等等)关于的事实清除()在.NET组件容器荷兰国际集团的项目没有处置他们(通过调用Dispose(的真正的)
There are multiple threads(a, b, c etc.) about the fact that Clear() ing items in the .NET component containers does not Dispose them(by calling Dispose(true).
最常见的,恕我直言,清除-ED组件不再使用的应用程序,因此它需要显式地从父容器清除它们后处理完毕。
Most frequently, IMHO, the Clear-ed components are not used anymore in the application, so it needs explicitly be Disposed after Clearing them from the parent containers.
也许是个不错的想法,收集的清除
方法有一个布尔参数处理
,当真正的还部署了它从列表中删除之前集合中的元素?
Maybe is a good idea that collection's Clear
method had a bool parameter dispose
that when in true also disposes the collection elements before its removing from the list?
推荐答案
问计这样的修改是没有意义的,Windows窗体团队已经解散前一段时间。它是在维护模式下,只有安全问题和操作系统不兼容的考虑。
Asking for modifications like this is pointless, the Windows Forms team has been disbanded quite a while ago. It is in maintenance mode, only security issues and OS incompatibilities are considered.
这是其他方式很简单创建自己的方法来做到这一点:
It is otherwise simple enough to create your own method to do this:
public static class ExtensionMethods {
public static void Clear(this Control.ControlCollection controls, bool dispose) {
for (int ix = controls.Count - 1; ix >= 0; --ix) {
if (dispose) controls[ix].Dispose();
else controls.RemoveAt(ix);
}
}
}
现在,你可以这样写:
panel1.Controls.Clear(true);
这篇关于清除控制不处置这些 - 什么是风险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!