#if FAT using System; namespace LinqInternal.Threading { [System.Diagnostics.DebuggerNonUserCode] internal static class DisposableExtensions { public static void DisposableSafeWith(this T disposable, Action action) where T : IExtendedDisposable { if (!ReferenceEquals(disposable, null)) { disposable.DisposedConditional ( null, () => { if (action != null) { action.Invoke(disposable); } }); } } public static TReturn DisposableSafeWith(this T disposable, Func action, TReturn def) where T : IExtendedDisposable { if (!ReferenceEquals(disposable, null)) { return disposable.DisposedConditional ( null, () => { if (action == null) { return def; } else { return action.Invoke(disposable); } } ); } else { return def; } } public static TReturn DisposableSafeWith(this T disposable, Func action, Func alternative, TReturn def) where T : IExtendedDisposable { if (!ReferenceEquals(disposable, null)) { return disposable.DisposedConditional ( null, () => { if (action == null) { return def; } else { return action.Invoke(disposable); } } ); } else { return alternative == null ? alternative.Invoke() : def; } } public static void DisposedConditional(this IExtendedDisposable disposable, string exceptionMessageWhenDisposed, Action whenNotDisposed) { Action whenDisposed = () => { throw new ObjectDisposedException(exceptionMessageWhenDisposed); }; if (disposable == null) { whenDisposed(); } else { disposable.DisposedConditional ( whenDisposed, whenNotDisposed ); } } public static TReturn DisposedConditional(this IExtendedDisposable disposable, string exceptionMessageWhenDisposed, Func whenNotDisposed) { Func whenDisposed = () => { throw new ObjectDisposedException(exceptionMessageWhenDisposed); }; if (disposable == null) { return whenDisposed(); } else { return disposable.DisposedConditional ( whenDisposed, whenNotDisposed ); } } public static void Run(Func allocationCode, Action bodyCode) { IDisposable resource = null; try { try { //Empty } finally { if (allocationCode != null) { resource = allocationCode.Invoke(); } } if ((bodyCode != null) && (resource != null)) { bodyCode.Invoke(resource); } } finally { if (resource != null) { resource.Dispose(); } } } public static void SafeWith(this T obj, Action action) { if ((action != null) && !ReferenceEquals(obj, null)) { action.Invoke(obj); } } public static TReturn SafeWith(this T obj, Func action, TReturn def) { if (!ReferenceEquals(obj, null)) { if (action == null) { return def; } else { return action.Invoke(obj); } } else { return def; } } public static TReturn SafeWith(this T obj, Func action, Func alternative, TReturn def) { if (!ReferenceEquals(obj, null)) { if (action == null) { if (alternative == null) { return def; } else { return alternative.Invoke(); } } else { return action.Invoke(obj); } } else { if (alternative == null) { return def; } else { return alternative.Invoke(); } } } public static void With(this T obj, Action action) { if (action == null) { throw new ArgumentNullException("action"); } action.Invoke(obj); } public static TReturn With(this T obj, Func action) { if (action == null) { throw new ArgumentNullException("action"); } return action.Invoke(obj); } } } #endif