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?
views:
43answers:
3
A:
I don't believe there's a standard for that, I would return the type of the object along with the string content of a unique identifying field (a 'Primary Key' of sorts).
Aviad P.
2009-12-26 22:12:23
+2
A:
I believe the recommended practice is to throw the following:
throw new ObjectDisposedException(GetType().FullName);
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.
Noldorin
2009-12-26 22:25:58
Also, please see this question and my answer for general guidelines: http://stackoverflow.com/questions/668440/handling-objectdisposedexception-correctly-in-an-idisposable-class-hierarchy
Noldorin
2009-12-26 22:28:07
+1
A:
Even the .NET Framework itself isn't very consistent here.
David M. Kean (former developer on the FxCop team at Micrsoft) added a comment to the MSDN documentation for the ObjectDisposedException:
The typical usage of this type is something like the following:
[C#] private void CheckDisposed() { throw new ObjectDisposedException(GetType().FullName); }
Albic
2009-12-26 22:26:57