.net - 我必须在 FolderBrowserDialog 上调用 Dispose 方法吗?

标签 .net idisposable

.NET Framework 中的 FolderBrowserDialog 组件(和 OpenFileDialog)实现了 IDisposible 接口(interface),这意味着我们应该调用它的 Dispose 方法在我们完成它或发生一些不好的事情(非托管资源泄漏)之后的某个适当的时间。

在 Visual Studio WinForm 设计器中,如果我将 FolderBrowserDialog 组件拖到 Form 上,设计器生成的代码似乎根本没有处理这个问题,没有代码调用 Dispose 方法FolderBrowserDialog

相比之下,如果我拖动一个 Timer(System.Windows.Forms 命名空间中的那个),它也实现了 IDisposible 接口(interface),生成的代码将是:

this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);

通过将计时器与容器 (this.components) 相关联,可以保证在处置容器时正确处置计时器 - 发生在 Form.Close()Form 时。 Dispose() 被调用。

那么为什么 FolderBrowserDialog 组件接受这种特殊处理?

最佳答案

好地方!原因可能是 FolderBrowserDialog 没有提供接受 IContainer 参数的构造函数,而 Timer 有。

至于为什么会这样,只能问winforms的原设计者了。也许它并不是真正设计用于以这种方式在设计器中使用的?他们只是意味着它可以在 using 语句中的代码中使用?

请记住,FolderBrowserDialog 及其父项实际上并没有覆盖 Component 中的 Dispose,因此它实际上没有 < em>需要处理任何东西。

关于.net - 我必须在 FolderBrowserDialog 上调用 Dispose 方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7637488/

相关文章:

C# 通过 SSH.NET 发送 Ctrl+Y

.net - 功能范式的效率

c# - 处理 ImageList

.net - WPF 窗口类的 IDisposable 成员

.net - 调用 Dispose 方法会清除和压缩 .Net 中的内存吗?

c# - PostSharp 切入点

.net - 将 .NET DateTime 值传递给 SAP PI xsd :Date

c# - 扩展原始类型

c# - 仅针对托管资源的最小 IDisposable implimenation

c# - 动态 COM 对象是否被视为托管资源?