andrew4582

DisposableAction

Dec 3rd, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Common.Utilities
  8. {
  9.     /// <summary>
  10.     /// Calls the provided action when Disposed is called. Used to remove try/finally statments
  11.     /// </summary>
  12.     /// <example>
  13.     /// <![CDATA[
  14.     /// using (new DisposableAction(() => "Do something on Dispose()"))
  15.     /// {
  16.     ///     //code here
  17.     /// }
  18.     /// ]]>
  19.     /// </example>
  20.     public class DisposableAction : IDisposable
  21.     {
  22.         readonly Action _action;
  23.  
  24.         public DisposableAction(Action action)
  25.         {
  26.             _action = action;
  27.         }
  28.  
  29.         public void Dispose()
  30.         {
  31.             _action();
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment