215 lines
7.1 KiB
C#
Executable File
215 lines
7.1 KiB
C#
Executable File
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Opsoft.NetCore.O3App.Shared
|
|
{
|
|
public static class Guard
|
|
{
|
|
public static T ArgumentNotNull<T>(T argumentValue, string argumentName)
|
|
where T : class
|
|
=> argumentValue ?? throw new ArgumentNullException(argumentName);
|
|
|
|
public static string ArgumentNotNullOrEmpty(string argumentValue, string argumentName)
|
|
{
|
|
if (string.IsNullOrEmpty(argumentValue ?? throw new ArgumentNullException(argumentName)))
|
|
{
|
|
throw new ArgumentException($"Specified argument is not allowed be an empty string.", argumentName);
|
|
}
|
|
return argumentValue;
|
|
}
|
|
|
|
public static string ArgumentNotNullOrWhiteSpace(string argumentValue, string argumentName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(argumentValue ?? throw new ArgumentNullException(argumentName)))
|
|
{
|
|
throw new ArgumentException($"Specified argument is not allowed be a white space string.", argumentName);
|
|
}
|
|
return argumentValue;
|
|
}
|
|
|
|
public static IEnumerable<TElement> ArgumentNotNullOrEmpty<TElement>(IEnumerable<TElement> argumentValue, string argumentName)
|
|
{
|
|
if (!(argumentValue ?? throw new ArgumentNullException(argumentName)).Any())
|
|
{
|
|
throw new ArgumentException($"Specified argument is not allowed be an empty collection.", argumentName);
|
|
}
|
|
return argumentValue;
|
|
}
|
|
|
|
public static Guid ArgumentNotEmpty(Guid argumentValue, string argumentName)
|
|
{
|
|
if (argumentValue == Guid.Empty)
|
|
{
|
|
throw new ArgumentException($"Specified argument is not allowed be an empty GUID.", argumentName);
|
|
}
|
|
return argumentValue;
|
|
}
|
|
|
|
public static Type ArgumentNotNullAndAssignableFrom(Type argumentValue, Type assignableFrom, string argumentName)
|
|
{
|
|
if (!assignableFrom.IsAssignableFrom(ArgumentNotNull(argumentValue, argumentName)))
|
|
{
|
|
throw new ArgumentException($"Specified argument type '{assignableFrom.FullName}' is not assinable from '{argumentValue.FullName}'.", argumentName);
|
|
}
|
|
return argumentValue;
|
|
}
|
|
|
|
public static void NoNullElements<T>(IEnumerable<T> objects, string argumentName)
|
|
{
|
|
foreach (object obj in objects)
|
|
{
|
|
if (obj == null)
|
|
{
|
|
throw new ArgumentNullException($"element can't be null. argumentName: {argumentName}");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void NoNullOrWhiteSpaceElements(IEnumerable<string> elements, string argumentName)
|
|
{
|
|
foreach (string element in elements)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(element))
|
|
{
|
|
throw new ArgumentNullException($"element can't be null or whitespace. argumentName: {argumentName}");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void For<TException>(Func<bool> predicate, string message)
|
|
where TException : Exception, new()
|
|
{
|
|
if (message == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(message));
|
|
}
|
|
|
|
TException exception = CreateException<TException>(message);
|
|
|
|
For(predicate, exception);
|
|
}
|
|
|
|
public static void For<TException>(Func<bool> predicate, Enum @enum)
|
|
where TException : Exception, new()
|
|
{
|
|
For(predicate, CreateException<TException>(@enum));
|
|
}
|
|
|
|
public static void For<TException>(Func<bool> predicate, TException exception)
|
|
where TException : Exception, new()
|
|
{
|
|
if (predicate == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(predicate));
|
|
}
|
|
|
|
if (exception == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(exception));
|
|
}
|
|
|
|
bool conditionNotMet = predicate.Invoke();
|
|
if (conditionNotMet)
|
|
{
|
|
throw exception;
|
|
}
|
|
}
|
|
|
|
private static TException CreateException<TException>(string message = null)
|
|
where TException : Exception, new()
|
|
{
|
|
if (String.IsNullOrWhiteSpace(message))
|
|
{
|
|
return new TException();
|
|
}
|
|
|
|
try
|
|
{
|
|
return (TException)Activator.CreateInstance(typeof(TException), message);
|
|
}
|
|
catch (MissingMethodException)
|
|
{
|
|
return new TException();
|
|
}
|
|
}
|
|
|
|
private static TException CreateException<TException>(params object[] args)
|
|
where TException : Exception, new()
|
|
{
|
|
try
|
|
{
|
|
return (TException)Activator.CreateInstance(typeof(TException), args);
|
|
}
|
|
catch (MissingMethodException)
|
|
{
|
|
return new TException();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 检查断言是否为真,否则抛出 <see cref="ArgumentException"/>。
|
|
/// </summary>
|
|
public static void Require(bool assertion, string message)
|
|
{
|
|
if (!assertion) throw new ArgumentException(message);
|
|
}
|
|
/// <summary>
|
|
/// 检查断言是否为真,否则抛出 <see cref="ArgumentException"/>。
|
|
/// </summary>
|
|
public static void Require(bool assertion, string message, string paramName)
|
|
{
|
|
if (!assertion) throw new ArgumentException(message, paramName);
|
|
}
|
|
/// <summary>
|
|
/// 检查断言是否为真,否则抛出 <see cref="ArgumentException"/>。
|
|
/// </summary>
|
|
public static void Require(bool assertion, string message, string paramName, Exception innerException)
|
|
{
|
|
if (!assertion) throw new ArgumentException(message, paramName, innerException);
|
|
}
|
|
/// <summary>
|
|
/// 检查断言是否为真,否则抛出 <see cref="ArgumentException"/>。
|
|
/// </summary>
|
|
public static void Require(bool assertion, string message, Exception innerException)
|
|
{
|
|
if (!assertion) throw new ArgumentException(message, innerException);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static class GuardEx
|
|
{
|
|
|
|
public static void For<TException>(Func<bool> predicate, params object[] args)
|
|
where TException : Exception, new()
|
|
{
|
|
if (args == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(args));
|
|
}
|
|
|
|
TException exception = CreateException<TException>(args);
|
|
|
|
Guard.For<TException>(predicate, exception);
|
|
}
|
|
|
|
|
|
private static TException CreateException<TException>(params object[] args)
|
|
where TException : Exception, new()
|
|
{
|
|
try
|
|
{
|
|
return (TException)Activator.CreateInstance(typeof(TException), args);
|
|
}
|
|
catch (MissingMethodException)
|
|
{
|
|
return new TException();
|
|
}
|
|
}
|
|
}
|
|
}
|