问题描述
在实现IDisposable时,我理解在处置对象后不应调用的每个方法都应抛出 ObjectDisposedException
。但是应该传递给异常的构造函数的名称对象的标准是什么?
When implementing IDisposable, I undertand that every method that shouldn't be called after the object's been disposed should throw the ObjectDisposedException
. But what is the standard for the name object that should be passed to the exception's constructor?
推荐答案
我相信推荐的做法是抛出以下内容:
I believe the recommended practice is to throw the following:
throw new ObjectDisposedException(GetType().FullName);
或者包括支票在内,这两种代码行都在每种需要它的方法的顶部(显然而不是 Dispose
方法本身):
Or including the check, these two lines of code at the top of each method that needs it (obviously not the Dispose
method itself):
if (this.disposed)
throw new ObjectDisposedException(GetType().FullName);
甚至可能有助于将其重构为一种微小的可用性方法。
Might even be helpful to refactor this into a tiny method for usability.
这篇关于抛出ObjectDisposedException时应以什么作为objectName传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!