ActionDisposable.cs 730 B

12345678910111213141516171819202122232425262728
  1. using System;
  2. namespace Wayne.Lib
  3. {
  4. /// <summary>
  5. /// A disposable where a behavior can be invoked when dispose occurs.
  6. /// The dispose action will only be invoked once regardless number of times
  7. /// the Dispose method is called.
  8. /// </summary>
  9. public class ActionDisposable : DisposableBase
  10. {
  11. private readonly Action action;
  12. /// <summary>
  13. /// Constructor.
  14. /// </summary>
  15. /// <param name="action">Action to be invoked first time the object is disposed.</param>
  16. public ActionDisposable(Action action)
  17. {
  18. this.action = action;
  19. }
  20. protected override void DoDispose()
  21. {
  22. action();
  23. }
  24. }
  25. }