Consider the following IDisposable
class:
class MyClass : IDisposable
{
public bool IsDisposed { get; private set; } = false;
public void Dispose()
{
IsDisposed = true;
}
}
Every method in this class, including Dispose()
, should begin with a check like this:
if (IsDisposed)
{
throw new ObjectDisposedException(...);
}
Since it is tedious and repetitive to write this in all methods, I would like to use contract invariant:
public class MyClass : IDisposable
{
...
[ContractInvariantMethod]
private void objectInvariant()
{
Contract.Invariant(!IsDisposed)
}
...
}
However, this only ensures IsDisposed is false at the end of each public method, excluding Dispose()
.
Once Dispose()
is called, the check should be done at the beginning of each method (including Dispose()
). Otherwise the obejct will be in invalid state during the method run, potentially leading to difficult bugs.
Hence contract invariants are not really usable for IDisposable
. Or am I missing something?
Is it possible to force invaraiants to be also used as preconditions or do I really have to write the same precondition (!IsDisposed
) to all methods manually?