JD2022-TU1/main/tools/framework/JD.Kernel/Classes/Scope.cs

64 lines
1.2 KiB
C#

using System;
using JD.Kernel.StaticHelpers;
namespace JD.Kernel.Classes
{
public class Scope
{
#region Variables
private int m_level;
#endregion
#region Properties
public bool InScope
{
get { return m_level > 0; }
}
#endregion
#region Methods
public virtual IDisposable Begin()
{
return Begin(null, null);
}
protected IDisposable Begin(Action begin, Action end)
{
++m_level;
if (m_level == 1)
{
Delegates.SafeExecute(begin);
}
return new NotFinalizableHelper(delegate
{
System.Diagnostics.Debug.Assert(m_level > 0);
if (--m_level == 0)
{
Delegates.SafeExecute(end);
OnEndScope(EventArgs.Empty);
}
});
}
#endregion
#region Event
public event EventHandler EndScope;
protected virtual void OnEndScope(EventArgs e)
{
EndScope?.Invoke(this, e);
}
#endregion
}
}