jellyfin/RSSDP/DisposableManagedObjectBase.cs

77 lines
3.0 KiB
C#
Raw Normal View History

using System;
2016-10-29 18:22:20 -04:00
using System.Collections.Generic;
2021-02-14 09:11:46 -05:00
using System.Globalization;
2016-10-29 18:22:20 -04:00
using System.Text;
namespace Rssdp.Infrastructure
{
2019-01-07 18:24:34 -05:00
/// <summary>
2020-11-18 08:46:14 -05:00
/// Correctly implements the <see cref="IDisposable"/> interface and pattern for an object containing only managed resources, and adds a few common niceties not on the interface such as an <see cref="IsDisposed"/> property.
2019-01-07 18:24:34 -05:00
/// </summary>
public abstract class DisposableManagedObjectBase : IDisposable
{
/// <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);
/// <summary>
2019-01-13 15:37:13 -05:00
/// Throws and <see cref="ObjectDisposedException"/> if the <see cref="IsDisposed"/> property is true.
2019-01-07 18:24:34 -05:00
/// </summary>
/// <seealso cref="IsDisposed"/>
2019-01-13 15:37:13 -05:00
/// <exception cref="ObjectDisposedException">Thrown if the <see cref="IsDisposed"/> property is true.</exception>
2019-01-07 18:24:34 -05:00
/// <seealso cref="Dispose()"/>
protected virtual void ThrowIfDisposed()
{
2020-06-20 04:35:29 -04:00
if (this.IsDisposed)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
2019-01-07 18:24:34 -05:00
}
/// <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;
}
2020-06-20 05:13:48 -04:00
2018-09-12 13:26:21 -04:00
public string BuildMessage(string header, Dictionary<string, string> values)
{
var builder = new StringBuilder();
2016-10-29 18:22:20 -04:00
2020-09-16 08:16:44 -04:00
const string ArgFormat = "{0}: {1}\r\n";
2018-09-12 13:26:21 -04:00
2021-02-14 09:11:46 -05:00
builder.AppendFormat(CultureInfo.InvariantCulture, "{0}\r\n", header);
2018-09-12 13:26:21 -04:00
foreach (var pair in values)
{
2021-02-14 09:11:46 -05:00
builder.AppendFormat(CultureInfo.InvariantCulture, ArgFormat, pair.Key, pair.Value);
2018-09-12 13:26:21 -04:00
}
builder.Append("\r\n");
return builder.ToString();
}
2019-01-07 18:24:34 -05:00
2018-09-12 13:26:21 -04:00
/// <summary>
/// Disposes this object instance and all internally managed resources.
/// </summary>
/// <remarks>
2020-11-18 08:46:14 -05:00
/// <para>Sets the <see cref="IsDisposed"/> property to true. Does not explicitly throw an exception if called multiple times, but makes no promises about behavior of derived classes.</para>
2018-09-12 13:26:21 -04:00
/// </remarks>
/// <seealso cref="IsDisposed"/>
2020-11-18 08:46:14 -05:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "We do exactly as asked, but CA doesn't seem to like us also setting the IsDisposed property. Too bad, it's a good idea and shouldn't cause an exception or anything likely to interfere with the dispose process.")]
2019-01-07 18:24:34 -05:00
public void Dispose()
{
2018-09-12 13:26:21 -04:00
IsDisposed = true;
2016-10-29 18:22:20 -04:00
2018-09-12 13:26:21 -04:00
Dispose(true);
}
}
2019-01-07 18:24:34 -05:00
}