using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System;
///
/// Throw helper extensions for .
///
internal static class ObjectDisposedExceptionExtensions
{
///
/// Throws an if is .
///
/// Dummy value to invoke the extension upon (always pass .
/// The condition indicating whether has been disposed.
/// The instance that can be signaled as being disposed.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIf(this ObjectDisposedException? _, [DoesNotReturnIf(true)] bool condition, object disposableInstance)
{
if (condition)
{
Throw(disposableInstance);
}
}
///
/// Throws an if is .
///
/// Dummy value to invoke the extension upon (always pass .
/// The instance to check to determine whether the target instance has been disposed.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNull(this ObjectDisposedException? _, [NotNull] object? validationObject)
{
if (validationObject is null)
{
Throw();
}
}
///
/// Throws an .
///
/// Thrown with no parameters.
[DoesNotReturn]
private static void Throw()
{
throw new ObjectDisposedException(null);
}
///
/// Throws an for a given target instance.
///
/// The target instance that was disposed.
/// Thrown for .
[DoesNotReturn]
private static void Throw(object targetObject)
{
throw new ObjectDisposedException(targetObject.ToString());
}
}