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) {
DefaultInterpolatedStringHandler val = default(DefaultInterpolatedStringHandler);
val..ctor(40, 1);
val.AppendLiteral("The given number is less or equal than: ");
val.AppendFormatted<int>(reference);
throw new ArgumentException(val.ToStringAndClear());
}
}
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)) {
DefaultInterpolatedStringHandler val = default(DefaultInterpolatedStringHandler);
val..ctor(9, 2);
val.AppendFormatted("obj");
val.AppendLiteral(" is not ");
val.AppendFormatted<Type>(TypeCache<TType>.Type);
val.AppendLiteral(".");
throw new ArgumentException(val.ToStringAndClear(), "obj");
}
}
internal static void EnsureTypeMapIsValid(Type serviceType, Type implementationType)
{
EnsureIsResolvable(implementationType);
if (!implementationType.Implements(serviceType)) {
DefaultInterpolatedStringHandler val = default(DefaultInterpolatedStringHandler);
val..ctor(47, 2);
val.AppendLiteral("The type ");
val.AppendFormatted<Type>(implementationType);
val.AppendLiteral(" does not implement the service type ");
val.AppendFormatted<Type>(serviceType);
val.AppendLiteral(".");
throw new InvalidRegistrationException(implementationType, val.ToStringAndClear(), null);
}
}
internal static void EnsureIsResolvable(Type implementationType)
{
if (!implementationType.IsResolvableType()) {
DefaultInterpolatedStringHandler val = default(DefaultInterpolatedStringHandler);
val..ctor(94, 1);
val.AppendLiteral("The type ");
val.AppendFormatted<Type>(implementationType);
val.AppendLiteral(" could not be resolved. It's probably an interface, abstract class or primitive type.");
throw new InvalidRegistrationException(implementationType, val.ToStringAndClear(), null);
}
}
internal static void ThrowDisposedException([System.Runtime.CompilerServices.Nullable(2)] string name, string caller)
{
DefaultInterpolatedStringHandler val = default(DefaultInterpolatedStringHandler);
val..ctor(64, 2);
val.AppendLiteral("The member '");
val.AppendFormatted(caller);
val.AppendLiteral("' was called on '");
val.AppendFormatted(name);
val.AppendLiteral("' but it has been disposed already.");
throw new ObjectDisposedException(name, val.ToStringAndClear());
}
}
}