jellyfin-server/Emby.Server.Implementations/Net/DisposableManagedObjectBase.cs

67 lines
2.3 KiB
C#
Raw Normal View History

using System;
2016-11-04 08:31:05 +00:00
namespace Emby.Server.Implementations.Net
2016-11-04 08:31:05 +00:00
{
/// <summary>
/// Correclty implements the <see cref="IDisposable"/> interface and pattern for an object containing only managed resources, and adds a few common niceities not on the interface such as an <see cref="IsDisposed"/> property.
/// </summary>
public abstract class DisposableManagedObjectBase : IDisposable
{
#region Public Methods
/// <summary>
/// Override this method and dispose any objects you own the lifetime of if disposing is true;
/// </summary>
/// <param name="disposing">True if managed objects should be disposed, if false, only unmanaged resources should be released.</param>
protected abstract void Dispose(bool disposing);
//TODO Remove and reimplement using the IsDisposed property directly.
2016-11-04 08:31:05 +00:00
/// <summary>
2019-01-13 20:37:13 +00:00
/// Throws an <see cref="ObjectDisposedException"/> if the <see cref="IsDisposed"/> property is true.
2016-11-04 08:31:05 +00:00
/// </summary>
/// <seealso cref="IsDisposed"/>
2019-01-13 20:37:13 +00:00
/// <exception cref="ObjectDisposedException">Thrown if the <see cref="IsDisposed"/> property is true.</exception>
2016-11-04 08:31:05 +00:00
/// <seealso cref="Dispose()"/>
protected virtual void ThrowIfDisposed()
{
if (IsDisposed) throw new ObjectDisposedException(GetType().Name);
2016-11-04 08:31:05 +00:00
}
#endregion
#region Public Properties
/// <summary>
/// Sets or returns a boolean indicating whether or not this instance has been disposed.
/// </summary>
/// <seealso cref="Dispose()"/>
public bool IsDisposed
{
get;
private set;
}
#endregion
#region IDisposable Members
/// <summary>
/// Disposes this object instance and all internally managed resources.
/// </summary>
/// <remarks>
/// <para>Sets the <see cref="IsDisposed"/> property to true. Does not explicitly throw an exception if called multiple times, but makes no promises about behaviour of derived classes.</para>
/// </remarks>
/// <seealso cref="IsDisposed"/>
public void Dispose()
{
2018-09-12 17:26:21 +00:00
IsDisposed = true;
2016-11-04 08:31:05 +00:00
2018-09-12 17:26:21 +00:00
Dispose(true);
2016-11-04 08:31:05 +00:00
}
#endregion
}
}