65 lines
1.4 KiB
C#
65 lines
1.4 KiB
C#
using System;
|
|
|
|
namespace JD.Kernel.Classes
|
|
{
|
|
/// <summary>
|
|
/// Simple class that implements NotFinalizable using a delegate.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Can be useful to implement a "using pattern" that must absolutly be disposed explicitly without defining a nested class.
|
|
/// </remarks>
|
|
/// <example>
|
|
/// <code>
|
|
/// public class MyClass
|
|
/// {
|
|
/// public IDisposable Start()
|
|
/// {
|
|
/// return new NotFinalizableHelper(delegate
|
|
/// {
|
|
/// // ... End code.
|
|
/// });
|
|
/// }
|
|
/// }
|
|
///
|
|
/// using (MyClass.Start())
|
|
/// {
|
|
/// ...
|
|
/// }
|
|
/// </code>
|
|
/// </example>
|
|
public class NotFinalizableHelper : NotFinalizable
|
|
{
|
|
#region Variables
|
|
|
|
private readonly Action m_action;
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
|
|
/// <summary>
|
|
/// Constructor.
|
|
/// </summary>
|
|
/// <param name="action"></param>
|
|
public NotFinalizableHelper(Action action)
|
|
{
|
|
m_action = action;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Overriden.
|
|
/// </summary>
|
|
/// <param name="disposing"></param>
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing && m_action != null)
|
|
{
|
|
m_action();
|
|
}
|
|
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|