Created
August 20, 2022 19:40
-
-
Save jfoshee/04af9707b613a487b5159f3c722007dc to your computer and use it in GitHub Desktop.
Convenient wrapper for an Action that must be called in spite of exceptions. Use with a `using` statement.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/// <summary> | |
/// Convenient wrapper for an Action that must be called in spite of exceptions. | |
/// Use with a `using` statement. | |
/// <code> | |
/// using var _ = new ActionDisposable(action); | |
/// </code> | |
/// </summary> | |
public class ActionDisposable : IDisposable | |
{ | |
private readonly Action disposeAction; | |
public ActionDisposable(Action disposeAction) | |
{ | |
this.disposeAction = disposeAction ?? throw new ArgumentNullException(nameof(disposeAction)); | |
} | |
public void Dispose() => disposeAction(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment