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);
这篇关于明确的控制并不能消除它们——风险是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:明确的控制并不能消除它们——风险是什么?
基础教程推荐
- WPF 模态进度窗口 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
