明确的控制并不能消除它们——风险是什么?

Clear controls does not dispose them - what is the risk?(明确的控制并不能消除它们——风险是什么?)

本文介绍了明确的控制并不能消除它们——风险是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有多个线程(a, b, c 等)关于 Clear() .NET 组件容器中的项目不 Dispose(通过调用 Dispose(true)).

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).

恕我直言,最常见的情况是,应用程序中不再使用已清除的组件,因此需要在从父容器中清除它们后显式处置.

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.

也许集合的 Clear 方法有一个布尔参数 dispose 是个好主意,当它为 true 时也会在集合元素从列表中删除之前对其进行处置?

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 Forms 团队早就解散了.它处于维护模式,仅考虑安全问题和操作系统不兼容问题.

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);

这篇关于明确的控制并不能消除它们——风险是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:明确的控制并不能消除它们——风险是什么?

基础教程推荐