c# – 在具有Finalizer的类上调用.Dispose()

根据 Essential C# 6.0你应该:

AVOID calling dispose() on owned objects that have a finalizer.
Instead,rely on the finalization queue to clean up the instance.

>有人可以详细说明这一点,因为我不清楚dispose的重点是什么,如果我们不从拥有的对象中调用它?
>除了Reflection之外,你怎么知道对象是否有Finalizer?
>你如何知道何时调用Close()/ Close()dispose(),而不是搜索api文档(如果你有权访问它并且它存在)或反射?对于非常特定的类型(MemoryStream / Form / sqlConnection / etc),我在网络上看到很多问题,但我更关注“如何自己解决”.

根据Dispose Pattern你应该:

CONSIDER providing method Close(),in addition to the dispose(),if close is standard terminology in the area. When doing so,it is important that you make the Close implementation identical to dispose and consider implementing the Idisposable.dispose method explicitly.

但有时你应该像表格一样打电话.像“Close and Dispose – which to call?”这样的问题越来越接近,但除此之外没有明确的方法

As usual the answer is: it depends. Different classes implement Idisposable in different ways,and it’s up to you to do the necessary research.

编辑:这是完整的指南,我没有要求复制许可,但因为它是一个指南(因此假设它应该是自由共享的公共知识)而不是实际培训材料的某些部分,我希望我’我没有违反任何规则.

Guidelines
DO implement a finalizer method only on objects with resources that are scarce or expensive,even though finalization delays garbage collection.
DO implement Idisposable to support deterministic finalization on classes with finalizers.
DO implement a finalizer method on classes that implement Idisposable in case dispose() is not invoked explicitly.
DO refactor a finalization method to call the same code as Idisposable,perhaps simply calling the dispose() method.
DO NOT throw exceptions from finalizer methods.
DO call System.GC.SuppressFinalize() from dispose() to avoid repeating resource cleanup and delaying garbage collection on an object.
DO ensure that dispose() is idempotent (it should be possible to call dispose() multiple times).
DO keep dispose() simple,focusing on resource cleanup required by finalization.
AVOID calling dispose () on owned objects that have a finalizer. Instead,rely on the finalization queue to clean up the instance.
AVOID referencing other objects that are not being finalized during finalization.
DO invoke a base class’s dispose() method when overriding dispose().
CONSIDER ensuring that an object becomes unusable after dispose() is called. After an object has been disposed,methods other than dispose() (which Could potentially be called multiple times) should throw an ObjectdisposedException.
DO implement Idisposable on types that own disposable fields (or properties) and dispose of said instances.

解决方法

  1. Could someone please elaborate on this as I’m not clear on what the point of dispose is if we’re not to call it from owned objects?

如果没有书的全文及其背景(我没有书的副本,也没有其他人阅读你的问题),就不可能肯定地说出他们的意思.但它应该是一本好书,因此我必须假设你引用的文本仅适用于你自己的终结器中的代码.即当然你应该正常处置拥有的对象.在dispose()方法中.

这是关于如果你的对象没有妥善处理该怎么办.答案就是清理自己的非托管资源.

与此相关的是,现在,随着SafeHandle类的出现(不久前),您可能根本不需要终结器.相反,将您自己的非托管资源包装在SafeHandle子类中,让该类处理完成.

  1. Apart from Reflection,how would you figure out if the object has a Finalizer?

除了反思之外,你将依赖于源代码(如果可用),文档(如果编写),或者仅仅是对象实现Idisposable的事实(即做出假设……它不能保证,但是它之间有很强的相关性他们俩).

更重要的是,请注意,因为可以在不使用终结器的情况下正确实现实现Idisposable的对象(例如,如果您使用SafeHandle,或者只实现Idisposable以便您可以确定性地清理拥有的Idisposable对象),那么终结者不保证.

我认为更好的方式来指导指导将是“不要在你的终结器中处理对象”.依赖于Idisposable对象本身应以某种方式处理最终自己拥有的资源这一事实,并且只关注您自己的对象直接拥有的任何非托管资源.

  1. How do you figure out when to call Close() / Close() + dispose() other than searching the API documentation (if you have access to it and it exists) or Reflection? I see a lot of questions around the net for very specific types ( MemoryStream / Form / sqlConnection / etc ) but I’m looking more at “how to figure it out yourself”.

你不能.并非仔细检查代码.那说……

你永远不必同时调用Close()和dispose().如果正确实现了类,则两者应始终等效.

当然,.NET中没有任何内容可以强制执行.因此,不可能肯定地说你不需要.但是如果你正在处理一种既需要两者的类型,那么它写得很差,也可能在其他方面被打破.最好完全避免完全使用该类型.

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么