예제 #1
0
        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);
        }
예제 #3
0
        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();
        }
예제 #4
0
 // 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();
 }