是否Form.Dispose()呼叫控制内部的Dispose()方法?方法、Form、Dispose

2023-09-03 00:35:16 作者:刪了伱電腦都不卡了

当我创建一个表单,自动生成的code不包括overrided Dispose方法。这是否意味着处置没有被要求在表单中的所有控件?

When I create a Form, the auto-generated code doesn't include an overrided Dispose method. Does that mean Dispose is not being called for all the controls in the form?

推荐答案

当你调用处置的形式,它会调用处置在其控件收集每个控件。这些控件将反过来做同样的,所以最后所有控件的处置方法应被调用。注意,这不是基于控制措施是否present在设计与否;它是根据被发现的控制什么控制实例集合形式的时候调用处置完成

When you call Dispose on the form, it will call Dispose for each control in its Controls collection. Those controls will in turn do the same, so in the end all controls' Dispose method should have been invoked. Note that this is not based on whether the controls are present in the designer or not; it is based on what control instances that are found in the Controls collection of the form at the time the call to Dispose is done.

当我看到这种情况是不会发生的唯一情况是,如果你创建一些容器控制自己,并覆盖处置不传播调用无论是基类或者iterate过载控制和呼叫处置在他们身上。

The only case when I could see that this would not happen is if you create some container control yourself and override Dispose without propagating the call either to the base class or iterate over the contained controls and call Dispose on them.