Shield
Represents a utility class for input validation.
using Stashbox.Exceptions;
using System;
using System.Runtime.CompilerServices;
namespace Stashbox.Utils
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class Shield
{
public static void EnsureNotNull<[System.Runtime.CompilerServices.Nullable(2)] T>(T obj, string parameterName)
{
if (obj == null)
throw new ArgumentNullException(parameterName);
}
public static void EnsureNotNull<[System.Runtime.CompilerServices.Nullable(2)] T>(T obj, string parameterName, string message)
{
if (obj == null || obj.Equals(null))
throw new ArgumentNullException(parameterName, message);
}
public static void EnsureNotNullOrEmpty(string obj, string parameterName)
{
if (string.IsNullOrWhiteSpace(obj))
throw new ArgumentException(string.Empty, parameterName);
}
public static void EnsureGreaterThan(int actual, int reference)
{
if (reference >= actual)
throw new ArgumentException($"""{reference}");
}
public static void EnsureTrue(bool condition, string message)
{
if (!condition)
throw new ArgumentException(message);
}
public static void EnsureTypeOf<[System.Runtime.CompilerServices.Nullable(2)] TType>(object obj)
{
if (!(obj is TType))
throw new ArgumentException(string.Format("{0} is not {1}.", "obj", TypeCache<TType>.Type), "obj");
}
internal static void EnsureTypeMapIsValid(Type serviceType, Type implementationType)
{
EnsureIsResolvable(implementationType);
if (!implementationType.Implements(serviceType))
throw new InvalidRegistrationException(implementationType, $"""{implementationType}""{serviceType}""", null);
}
internal static void EnsureIsResolvable(Type implementationType)
{
if (!implementationType.IsResolvableType())
throw new InvalidRegistrationException(implementationType, $"""{implementationType}""", null);
}
internal static void ThrowDisposedException([System.Runtime.CompilerServices.Nullable(2)] string name, string caller)
{
throw new ObjectDisposedException(name, "The member '" + caller + "' was called on '" + name + "' but it has been disposed already.");
}
}
}