public static void OnDispose(this IDisposablePattern disposablePattern) { foreach (var disposeAction in disposablePattern.DisposeActions) { disposeAction(); } disposablePattern.DisposeActions.Clear(); disposablePattern.DisposeNativeResources(); }
/// <summary> /// Throws an exception if the <see cref="IDisposablePattern.Disposed"/> was true. /// </summary> public static void AssertNotDisposed(this IDisposablePattern disposable) { if (disposable is null) Error.ThrowIfNull(nameof(disposable)); string objectName; //Avoid excpetions that might be thrown by an overridden ToString(). try { objectName = disposable.ToString(); } catch { objectName = disposable.GetType().ToString(); } Error.ThrowObjectDisposed(disposable.Disposed, objectName); }
public static void NotDisposed(IDisposablePattern disposable, string objectName) { Assumes.NotNull(disposable); Assumes.NotNullOrEmpty(objectName); if (disposable.IsNotNull()) { if (disposable.Disposed) { throw ExceptionBuilder.CreateObjectDisposed(objectName); } } Contract.EndContractBlock(); }
// NOTE: Leave out the finalizer altogether if this class doesn't // own unmanaged resources, but leave the other methods // exactly as they are. public static void OnDestruct(this IDisposablePattern disposablePattern) { // Finalizer calls Dispose(false) disposablePattern.DisposeNativeResources(); }