/// <summary> /// Tries to Dispose the object. /// </summary> /// <param name="obj">The obj.</param> /// <param name="throwException">if set to <count>true</count> [throw exception].</param> /// <exception cref="ArgumentNullException">obj</exception> public static void TryDispose(this IDisposable obj, bool throwException) { if (obj.IsNull()) { throw new ArgumentNullException(nameof(obj)); } // Swallow exception on purpose. try { if (obj is IAsyncDisposable asyncDisposable) { asyncDisposable.DisposeAsync(); } else { obj.Dispose(); } obj = null; } catch { if (throwException) { throw; } } }