/// <summary> /// Throw an exception if the object is disposed /// </summary> /// <param name="disposableState"></param> public static void ThrowIfDisposed(this IDisposableState disposableState) { if (disposableState.Disposed) { throw new ObjectDisposedException($"{nameof(disposableState)} has been already disposed."); } }
public void TracedDisposable003() { { var tdc = new TracedDisposableControl(); tdc.SetTraceEnabledForAll(true); bool? disposeQ = null; var sut = new TestTracedDisposable((d) => { disposeQ = d; }, tdc); IDisposableState sutDS = sut; Assert.False(sutDS.IsDisposed()); Assert.False(sutDS.IsFinalizeSuppressed()); Assert.NotNull(sutDS.GetReportFinalizedInfo().CtorStackTrace); sut.Dispose(); Assert.True(sutDS.IsDisposed()); Assert.True(sutDS.IsFinalizeSuppressed()); Assert.Equal(true, disposeQ); } { var tdc = new TracedDisposableControl(); // NOT tdc.SetTraceEnabledForAll(true); bool? disposeQ = null; var sut = new TestTracedDisposable((d) => { disposeQ = d; }, tdc); IDisposableState sutDS = sut; Assert.False(sutDS.IsDisposed()); Assert.False(sutDS.IsFinalizeSuppressed()); Assert.Null(sutDS.GetReportFinalizedInfo().CtorStackTrace); sut.Dispose(); Assert.True(sutDS.IsDisposed()); Assert.True(sutDS.IsFinalizeSuppressed()); Assert.Equal(true, disposeQ); } }