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.
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.
First:
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) Declare private bool disposed; in both classes. Each class tracks its own this.disposed, and throws as needed.
2) Use protected bool Disposed { get; private set; } instead of a field. This would let the subclass check the disposed state.
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) This "smells" to me since it contains duplicated booleans, but seems to work fine. I often use this when subclassing other code.
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) 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?
Second:
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)
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?