问题描述
在的FolderBrowserDialog
组件在.NET框架(和打开文件对话框
)实现了 IDisposible
接口,这意味着我们要在某个适当的时候调用它的处置
方法后,我们用它做什么不好的事情发生(非托管资源泄漏)。
The FolderBrowserDialog
component in .NET Framework (and the OpenFileDialog
) implements the IDisposible
interface, meaning we should call its Dispose
method at some appropriate time after we've done with it or something bad happens (unmanaged resource leaks).
在Visual Studio中WinForm设计,如果我将一个的FolderBrowserDialog
组件到一个表格,由设计器生成的code似乎没有照顾这在总之,没有code调用Dispose方法上的的FolderBrowserDialog
。
In Visual Studio WinForm designer, if I drag a FolderBrowserDialog
component onto a Form, the code generated by the designer does not seem to take care of this at all, no code calls the Dispose method on the FolderBrowserDialog
.
在此相反,如果我将一个定时器
(一个在 System.Windows.Forms的
命名空间),这还实现了 IDisposible
接口,产生将是code:
In contrast, if I drag a Timer
(the one in the System.Windows.Forms
namespace) which also implements IDisposible
interface, the code generated would be:
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
通过定时器与容器(this.components)相关联,该定时器保证得到妥善处置容器时disposed-发生在 Form.Close()
或 Form.Dispose()
之称。
那么,为什么的FolderBrowserDialog
组件接收这个特殊的待遇?
So why FolderBrowserDialog
component receives this special treatment?
推荐答案
好去处!原因可能是,的FolderBrowserDialog
并没有提供一个构造函数的的IContainer
参数,而定时器
一样。
Good spot! The reason is probably that the FolderBrowserDialog
does not provide a constructor that takes an IContainer
argument, whereas Timer
does.
至于为什么会这样,你只能问原来的WinForms设计师。也许这是不是真的设计在设计以这种方式被使用?他们只是意味着它被用在code在使用
语句?
As to why this is so, you can only ask the original winforms designers. Maybe it isn't really designed to be used in the designer in this way? They only meant it to be used in code in a using
statement?
记住,的FolderBrowserDialog
,其父母在,不实际覆盖处置
从组件
,所以它实际上并没有的需要的处置任何东西。
Bear in mind that FolderBrowserDialog
, and its parents, don't actually override Dispose
from Component
, so it doesn't actually need to dispose anything.
这篇关于我必须调用Dispose方法上的FolderBrowserDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!