public static void TryValidateParam([NotNull] IEnumerable collection, string paramName, string message = "") { if (collection.Count() == 0) { message = CreateExceptionMessage(message, Resources.CollectionIsNullOrHasNoItems); ExceptionThrower.ThrowArgumentNullException(message, paramName); } }
public static bool TryValidateNull(object value, bool throwException = false) { if ((value is null) && throwException) { ExceptionThrower.ThrowInvalidValueException(Resources.ObjectValidationFailed, value); } return(value is null); }
public static bool TryValidateNull(object value, [NotNull] string message, bool throwException = false) { if ((value is null) && throwException) { ExceptionThrower.ThrowInvalidValueException(message, value); } return(value is null); }
public static void TryValidateParam(decimal value, decimal minimumValue, decimal maximumValue, string paramName, string message = "") { if (value.IsInRange(minimumValue, maximumValue) == false) { message = CreateExceptionMessage(message, Resources.NumberNotInRange); ExceptionThrower.ThrowArgumentOutOfRangeException(paramName, message); } }
public static void TryValidateParam([NotNull] Type value, [NotNull] Type expectedType, string paramName, string message = "") { if (value != expectedType) { message = CreateExceptionMessage(message, Resources.InvalidType); ExceptionThrower.ThrowArgumentInvalidException(message, paramName); } }
public static void TryValidateParam([NotNull] FileInfo file, string paramName, string message = "") { if (file.Exists == false) { message = CreateParamExceptionMessage(message, paramName, Resources.FileNotFound); ExceptionThrower.ThrowFileNotFoundException(message, file.FullName); } }
public static void TryValidateParam(string value, string paramName, string message = "") { if (string.IsNullOrEmpty(value)) { message = CreateExceptionMessage(message, Resources.StringIsEmpty); ExceptionThrower.ThrowArgumentNullException(message, paramName); } }
public static void TryValidateParam([NotNull] Guid value, string paramName, string message = "") { if (value.Equals(Guid.Empty)) { message = CreateExceptionMessage(message, Resources.GuidIsEmpty); ExceptionThrower.ThrowArgumentInvalidException(message, paramName); } }
public static void TryValidateParam(Uri value, string paramName, string message = "") { if (Validate.TryValidateNull(value)) { message = CreateExceptionMessage(message, Resources.UriCannotBeNull); ExceptionThrower.ThrowArgumentInvalidException(message, paramName); } }
public static void TryValidateParam([NotNull] Enum value, string paramName, string message = "") { if (Enum.IsDefined(value.GetType(), value) == false) { message = CreateExceptionMessage(message, Resources.TheValueIsNotDefinedInTheEnumeration); ExceptionThrower.ThrowArgumentOutOfRangeException(paramName, message); } }
public static void TryValidateParam <T>([NotNull] Span <T> span, string paramName, string message = "") { if (span.IsEmpty) { message = CreateExceptionMessage(message, Resources.SpanCannotBeNullOrEmpty); ExceptionThrower.ThrowArgumentNullException(message, paramName); } }
public static void TryValidateParam([NotNull] DirectoryInfo directory, string paramName, string message = "") { if (directory.Exists == false) { message = CreateParamExceptionMessage(message, paramName, Resources.DirectoryDoesNotExist); ExceptionThrower.ThrowDirectoryNotFoundException(message, directory?.FullName); } }
public static void AddLast <T>([NotNull] this IList <T> list, [NotNull] T item) { if (list.IsReadOnly) { ExceptionThrower.ThrowArgumentReadOnlyCollectionException(nameof(list)); } list.Insert(list.Count, item); }
public static void TryValidateParam(IEnumerable collection, int size, string paramName, string message = "") { TryValidateParam(collection, paramName, message); if (collection.Count() != size) { message = CreateExceptionMessage(message, Resources.CollectionSizeIsNotValid); ExceptionThrower.ThrowArgumentOutOfRangeException(paramName, message); } }
public static bool TryValidateValue(string input, bool throwException = false) { var result = string.IsNullOrEmpty(input); if (result is true && throwException) { ExceptionThrower.ThrowInvalidValueException(Resources.StringIsNotValid, input); } return(result); }
public static void TryValidateParam(string value, [NotNull] Regex match, string paramName, string message = "") { TryValidateParam(value, paramName, message); if (match.IsMatch(value) == false) { message = CreateExceptionMessage(message, Resources.StringIsNotValid); ExceptionThrower.ThrowArgumentInvalidException(message, paramName); } }
public static void TryValidateParam(string value, StringType stringType, int minimumLength, int maximumLength, string paramName, string message = "") { //TODO: Add more string types. TryValidateParam(value, paramName, message); TryValidateParam(stringType, nameof(stringType), Resources.InvalidStringType); if (value.Length.IsInRange(minimumLength, maximumLength) == false) { message = CreateExceptionMessage( message, string.Format( CultureInfo.CurrentCulture, Resources.InvalidStringLengthAcceptableRange, minimumLength, maximumLength)); ExceptionThrower.ThrowArgumentOutOfRangeException(paramName, message); } // Final string validation switch (stringType) { case StringType.Email: if (value.IsEmailAddress() == false) { message = CreateExceptionMessage(message, Resources.InvalidEmailAddress); ExceptionThrower.ThrowArgumentInvalidException(message, paramName); } break; case StringType.Url: if (value.IsUrl() == false) { message = CreateExceptionMessage(message, Resources.InvalidUrlAddress); ExceptionThrower.ThrowArgumentInvalidException(message, paramName); } break; case StringType.NotSet: break; default: break; } }
public static bool TryValidateValue <TValue>( TValue input, bool condition, string message = "", bool throwException = false) { if (condition is false && throwException) { message = CreateExceptionMessage(message, Resources.InvalidValue); ExceptionThrower.ThrowInvalidValueException <object>(message, input); } return(condition); }
/// <summary> /// Adds if not exists. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="list">The list.</param> /// <param name="item">The value.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> /// <exception cref="ArgumentNullException">list - List cannot be null. or value - Value cannot be null.</exception> /// <exception cref="ArgumentException">list - List cannot be null. or value - Value cannot be null.</exception> /// <exception cref="ArgumentNullException">list - List cannot be read-only.</exception> /// <exception cref="ArgumentException">list - List cannot be null. or value - Value cannot be null.</exception> public static bool AddIfNotExists <T>([NotNull] this ICollection <T> list, [NotNull] T item) { if (list.IsReadOnly) { ExceptionThrower.ThrowArgumentReadOnlyCollectionException(nameof(list)); } if (list.Contains(item)) { return(false); } list.Add(item); return(true); }
public static void TryValidateObject <TException>(bool condition, string message = "") where TException : Exception, new() { if (string.Equals(typeof(TException).Name, nameof(Exception), StringComparison.Ordinal)) { ExceptionThrower.ThrowArgumentInvalidException( "Exception is not allowed to be used for this method. Please choose a more detailed Exception type.", nameof(TException)); } if (condition is false) { message = CreateExceptionMessage(message, Resources.ObjectValidationFailed); var ex = Activator.CreateInstance(typeof(TException), message).As <TException>(); throw ex; } }
public static void TryValidateParam(string value, int minimumLength, int maximumLength, string paramName, string message = "") { TryValidateParam(value, paramName, message); TryValidateParam(minimumLength, 0, int.MaxValue, nameof(minimumLength)); TryValidateParam(maximumLength, 1, int.MaxValue, nameof(minimumLength)); if (value.Length.IsInRange(minimumLength, maximumLength) == false) { message = CreateExceptionMessage( message, string.Format( CultureInfo.CurrentCulture, Resources.InvalidStringLengthAcceptableRange, minimumLength, maximumLength)); ExceptionThrower.ThrowArgumentOutOfRangeException(paramName, message); } }