在IDisposable的类层次结构正确处理的ObjectDisposedException [英] Handling ObjectDisposedException correctly in an IDisposable class hierarchy

查看:135
本文介绍了在IDisposable的类层次结构正确处理的ObjectDisposedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当正确实施了IDisposable,大多数的实现,包括框架的指导方针,建议包括设置在私人布尔; 会员才能放心地让多次调用的Dispose()的Dispose(布尔)以及抛出<一个href="http://msdn.microsoft.com/en-us/library/system.objectdisposedexception.aspx">ObjectDisposedException在适当的时候。

When implementing IDisposable correctly, most implementations, including the framework guidelines, suggest including a private bool disposed; member in order to safely allow multiple calls to Dispose(), Dispose(bool) as well as to throw ObjectDisposedException when appropriate.

这对于一个类工作正常。但是,当你从你的可支配资源的子类,子类包含自己的本土资源和独特的方法,事情变得有点棘手。大部分样本显示如何覆盖 Dipose(BOOL处置)正确,但不超越,要处理的ObjectDisposedException

This works fine for a single class. However, when you subclass from your disposable resource, and a subclass contains its own native resources and unique methods, things get a little bit tricky. Most samples show how to override Dipose(bool disposing) correctly, but do not go beyond that to handling ObjectDisposedException.

有两个问题,我有过这种情况。

There are two questions that I have in this situation.


第一:

子类和基础类都需要能够跟踪处理的状态。有一对夫妇的主要选择,我知道的 -

The subclass and the base class both need to be able to track the state of disposal. There are a couple of main options I know of -

  • 1)声明私有布尔处置;在这两个班。每个类跟踪自己的this.disposed,并在需要时抛出。

  • 1) Declare private bool disposed; in both classes. Each class tracks its own this.disposed, and throws as needed.

2)使用受保护的布尔处置{获得;私定; },而不是一个场。这将让子类检查处理状态。

2) Use protected bool Disposed { get; private set; } instead of a field. This would let the subclass check the disposed state.

3)提供了一些保护的辅助方法来检查处理状态,并抛出经由反射拉动当前类型名称,如果该对象被设置。

3) Provide some protected helper method to check the disposed state, and throw by pulling the current type name via reflection if the object is disposed.

我看到每一个被选项的优点缺点是:

The advantages as disadvantages I see to each by option are:

  • 1)这种味道对我来说,因为它包含重复布尔值,但似乎很好地工作。我经常用这个继承等code时。

  • 1) This "smells" to me since it contains duplicated booleans, but seems to work fine. I often use this when subclassing other code.

2)取出复制布尔值,而不是设计指导书的编写方式等,这是我通常使用的,虽然,因为它保持它的状态,单点。

2) This takes out the duplicated booleans, but is not the way the design guidelines books are written, etc. This is what I typically use, though, since it keeps it a single point for state.

3)这似乎是最干净的选择我,但没有出现在标准的指导方针。这可能是少一点的预期比其他的方法从类的用户。

3) This seems like the cleanest option to me, but doesn't appear in standard guidelines. It may be a little less expected of an approach than others from users of the class.

我在一个点或另一个,都使用所有这三种方法尝试。我想知道的优点和缺点的三种方法,以及任何其他的想法更清洁,更好的方式来处理这个问题。你会在处理此做出什么选择,为什么?

I, at one point or another, have tried using all three of these approaches. I would like to know advantages and disadvantages to the three approaches, as well as any other ideas for a cleaner, better way to handle this. What choice would you make in handling this, and why?


二:

投掷当的ObjectDisposedException ,你用什么名称参数?我知道,典型的方法调用:

When throwing the ObjectDisposedException, what do you use for the name argument? I know the "typical" method call is:


throw new ObjectDisposedException(GetType().FullName);

有评论在此页面从微软员工表明实施具体类的全名是适当的使用。

There is a comment on this page from a Microsoft employee suggesting that implementing the concrete class's full name is the appropriate usage.

在上面的第三种选择,这将是唯一有意义的选择。然而,如果类实现投掷本身,则可能返回一个定义被调用的方法的类的名字。 (即:基类可以返回基类的名称,而不是具体子类)

In the third option above, this would be the only meaningful choice. However, if the class implements the throwing itself, you could potentially return the name of the class that defines the method that was called. (ie: the base class could return the base class's name, not the concrete subclass)

我不认为这是一个好主意 -​​ 但我遇到了这样的一些code写的别人。是否有优点和缺点,以具有类实现的方法返回的名称?

I don't think this is a good idea - but I ran into this on some code written by somebody else. Are there advantages or disadvantages to having the name of the class implementing the method returned?

推荐答案

我通常实现的第一个选项。事实上,这似乎是什么设计指导建议。原因是不会立即显现,但我认为这是一个很好的仍然:任何类实现者应该具有相同的排序防止该对象被设置为广大消费者的情况。换句话说,最好不要的假设的,无论谁正在实施一个派生类知道precisely时,他们可以调用一定的方法,其成功的执行也可以不依赖于对象是否已经被处置或被不(虽然这个理想应通过XML注释反正文件)。

I typically implement the first option. Indeed, it seems to be what the design guidelines recommend. The reason isn't immediately apparent, but I consider it a good one nonetheless: any implementer of the class should have the same sort of protection against the case where the object is disposed as general consumers. In other words, it's best not to assume that whoever is implementing a derived class knows precisely when they can call a certain method, whose successful execution may or may not depend on whether the object has already been disposed or not (though ideally this should be documented via XML comments anyway).

关于你提到的第二个问题,我会再次通过的GetType()。全名,特别是因为它的核心.NET框架中使用了推荐的做法坚持下去。即使你认为可供选择的方法较多appropiate,我认为这是最好坚持在.NET框架,为了保持一致性,使用的方法。

Regarding your second question, I would again stick with the recommended practice of passing GetType().FullName, especially since it's used in the core .NET framework. Even if you think alternative methods are more appropiate, I think it's best to stick to the method used in the .NET framework for the sake of consistency.

要得出结论:与所有的指导方针,这显然取决于你要如何实现一定的设计特征,但除非你有一个特别好的原因,这是非常可取的只是坚持下去。在这两种情况,它可能就不会做了很大的伤害,以利用一些您在您的文章提出的替代品,只要他们坚持使用和preferably记录给用户。

To conclude: as with all guidelines, it's clearly up to you how you want to implement a certain design feature, though unless you have a particularly good reason it's highly advisable just to stick with them. In both these situations, it probably wouldn't do a great deal of harm to utilise some of the alternatives you suggested in your post, so long as they are used consistently and preferably documented to the user.

这篇关于在IDisposable的类层次结构正确处理的ObjectDisposedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆