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

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)