using System; using System.Collections.Generic; using System.Linq; namespace Opsoft.NetCore.O3App.Shared { public static class Guard { public static T ArgumentNotNull(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 ArgumentNotNullOrEmpty(IEnumerable 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(IEnumerable 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 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(Func predicate, string message) where TException : Exception, new() { if (message == null) { throw new ArgumentNullException(nameof(message)); } TException exception = CreateException(message); For(predicate, exception); } public static void For(Func predicate, Enum @enum) where TException : Exception, new() { For(predicate, CreateException(@enum)); } public static void For(Func 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(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(params object[] args) where TException : Exception, new() { try { return (TException)Activator.CreateInstance(typeof(TException), args); } catch (MissingMethodException) { return new TException(); } } /// /// 检查断言是否为真,否则抛出 。 /// public static void Require(bool assertion, string message) { if (!assertion) throw new ArgumentException(message); } /// /// 检查断言是否为真,否则抛出 。 /// public static void Require(bool assertion, string message, string paramName) { if (!assertion) throw new ArgumentException(message, paramName); } /// /// 检查断言是否为真,否则抛出 。 /// public static void Require(bool assertion, string message, string paramName, Exception innerException) { if (!assertion) throw new ArgumentException(message, paramName, innerException); } /// /// 检查断言是否为真,否则抛出 。 /// 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(Func predicate, params object[] args) where TException : Exception, new() { if (args == null) { throw new ArgumentNullException(nameof(args)); } TException exception = CreateException(args); Guard.For(predicate, exception); } private static TException CreateException(params object[] args) where TException : Exception, new() { try { return (TException)Activator.CreateInstance(typeof(TException), args); } catch (MissingMethodException) { return new TException(); } } } }