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

前端之家收集整理的这篇文章主要介绍了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中没有任何内容可以强制执行.因此,不可能肯定地说你不需要.但是如果你正在处理一种既需要两者的类型,那么它写得很差,也可能在其他方面被打破.最好完全避免完全使用该类型.

猜你在找的C#相关文章