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);
这篇关于明确的控制并不能消除它们——风险是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:明确的控制并不能消除它们——风险是什么?
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01