使用基类的析构函数/Dispose?
在 C# 中,如文档中所述,以及这篇好文章的接受答案,据说类不会继承其父类的析构函数。
问题: 如果我想确保处置基类的私有元素,是否在所有子类中实现 IDisposable 并在 Dispose 方法中调用 base.Dispose() 的正确方法?
这样做看起来不错,但我更喜欢一种不需要在所有子类中实现的方法。
In C#, as mentioned in the Documentation, and this nice post's accepted answer, it's stated that classes don't inherit the Destructor of their parent class.
The question :
If I want to make sure to dispose the private elements of the base class, is the proper way to implement IDisposable in all the child class, and in the Dispose method, call base.Dispose()?
It looks alright to do so, but I would have preferred a way which wouldn't require implementation in all of the child classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MSDN 声明析构函数会自动在基类上调用。
MSDN states that destructors are called on base classes automatically.
您应该遵循此处的一次性模式。它还迎合了继承。
这里有趣的部分是“析构函数不继承”,我不知道该怎么理解。我写了一个小测试,但令我松了一口气的是,您只需要在基类中编写一个析构函数。
因此,子级与基类非托管资源分离。他们可以重写 Dispose(bool) 来清理自己的事务。
但我发表了评论,因为我看到太多程序员实现完整模式只是为了处理托管资源。
从一般设计的角度来看,Disposable 类最好是密封的。
You should follow the Disposable pattern here. It also caters for inheritance.
The interesting part here is "destructors don't inherit", I'm not sure what to make of that. I wrote a little test but to my relief you only need to write a destructor in the base-class.
So the children are uncoupled from the base-class unmanaged resources. They can override
Dispose(bool)
to cleanup their own affairs.But I made a comment because I see too many programmers implementing the full pattern just to handle managed resources.
From a general design perspective, a Disposable class would better be sealed.
基类应使用标准 IDisposable 模式 对于基类 - 这样只有基类需要终结器,派生类只需重写虚拟方法
The base class should use the standard IDisposable pattern for base classes - in this way only the base class needs a finalizer, and derived classes simply override the virtual method
如果父类使用 Microsoft 的 IDisposable 模式,则子类不应重写 Dispose(void),而应重写 Dispose(Boolean Disusing)。如果以 Dispose true 调用,则应处置子类并调用 base.Dispose(True)。如果使用 Dispose false 进行调用,则在大多数情况下除了调用 base.Dispose(False) 之外什么都不应该做。
请注意,在大多数情况下,对 base.Dispose(False) 的调用不会执行任何操作,但无论如何都应该进行。如果子类需要添加额外的“非托管资源”(即,如果它具有需要在终结器中执行的额外职责),那么它通常应该将这些职责封装到另一个对象中。请注意,类是否具有终结器的问题不仅仅是“实现细节”;而是一个问题。向基类添加终结器可能是一项重大更改,导致程序会泄漏资源(不好,但如果程序一次运行时间不长,则可能可以幸存)变成在随机情况下会泄漏资源的程序,尝试清理仍在使用的资源(通常不会导致问题,但可能会导致罕见但立即发生的故障)。
If the parent class uses Microsoft's IDisposable pattern, the child class should not override Dispose(void) but instead override Dispose(Boolean Disposing). If called with Disposing true, it should dispose the child class and call base.Dispose(True). If called with Disposing false, it should in most cases no nothing except call base.Dispose(False).
Note that in most cases, the call to base.Dispose(False) will do nothing, but it should be made anyway. If the child class would need to add additional "unmanaged resources" (i.e. if it has additional responsibilities that need to be performed in a finalizer) it should generally encapsulate those responsibilities into another object. Note that the question of whether a class has a finalizer is not just an "implementation detail"; adding a finalizer to a base class can be a breaking change, causing a program which would have leaked resources (bad, but perhaps survivable if it the program isn't run for too long at a time) into one which will, on random occasions, attempt to clean up resources which are still in use (won't often cause problems, but may cause rare but immediate failures).