using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainShell.Common
{
    /// <summary>
    /// 一个简单的包装类，在对象被 Dispose 时执行指定的 Action。
    /// </summary>
    public class DisposableAction : IDisposable
    {
        private readonly Action _action;
        private bool _isDisposed;

        public DisposableAction(Action action)
        {
            _action = action ?? throw new ArgumentNullException(nameof(action));
        }

        public void Dispose()
        {
            if (!_isDisposed)
            {
                _action();
                _isDisposed = true;
            }
        }
    }
}
