Ejemplo n.º 1
0
 /// <summary>
 /// Disposes the <paramref name="disposable"/> if it not null using the options specified.
 /// </summary>
 /// <param name="disposable">The item to dispose.</param>
 /// <param name="options">One or more values from <see cref="TryDisposeOptions"/> that specify details about how to perform the dispose.</param>
 public static void TryDispose(this IDisposable disposable, TryDisposeOptions options)
 {
     try
     {
         disposable?.Dispose();
     }
     catch (OutOfMemoryException) { throw; }
     catch
     {
         if ((options & TryDisposeOptions.SuppressExceptions) != TryDisposeOptions.SuppressExceptions)
         {
             throw;
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Disposes all items in an <see cref="IEnumerable{T}"/> that implement <see cref="IDisposable"/> Additionally, if the <paramref name="value"/> also implements <see cref="IDisposable"/> it too will be disposed.
        /// </summary>
        /// <remarks>
        /// <para>Null values will be ignored.</para>
        /// </remarks>
        /// <typeparam name="T">The type of value contained in the <see cref="IEnumerable{T}"/>. T must implement <see cref="IDisposable"/>.</typeparam>
        /// <param name="value">An <see cref="IEnumerable{T}"/> containing the items to be disposed.</param>
        /// <param name="options">One or more values from the <see cref="TryDisposeOptions"/> enum that specify rules for the disposal.</param>
        public static void TryDispose <T>(this IEnumerable <T> value, TryDisposeOptions options) where T : IDisposable
        {
            if (value == null)
            {
                return;
            }

            foreach (var item in value.Reverse())
            {
                item.TryDispose(options);
            }

            //If the enumerable itself is also disposable,
            //dispose it too.
            var disposableEnumerable = value as IDisposable;

            disposableEnumerable?.Dispose();
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Attempts to cast <paramref name="value"/> to <see cref="IDisposable"/> and if success disposes it using <see cref="TryDispose(IDisposable, TryDisposeOptions)"/>.
 /// </summary>
 /// <param name="value">The value to check &amp; dispose if it supports <see cref="IDisposable"/>.</param>
 /// <param name="options">One or more values from <see cref="TryDisposeOptions"/> that specify details about how to perform the dispose.</param>
 public static void TryDispose(this object value, TryDisposeOptions options)
 {
     TryDispose(value as IDisposable, options);
 }