public static void Validate <T>([AssertionCondition(AssertionConditionType.IS_NOT_NULL)][CanBeNull][ValidatedNotNull][NoEnumeration] this T value, [InvokerParameterName][NotNull] string argumentName, ObjectIs argumentValidationType) where T : class { if (ReferenceEquals(value, null) && ((argumentValidationType & ObjectIs.NotNull) == ObjectIs.NotNull)) { throw new ArgumentNullException(argumentName, ErrorMessages.ArgumentNull(argumentName)); } }
public static T[] Validate <T>([ItemCanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)][ValidatedNotNull] this IEnumerable <T> argumentValue, [NotNull][InvokerParameterName] string argumentName, ObjectIs argumentValidation, ObjectIs elementValidation) where T : class { /* * We should test "validation", but that would be an expensive thing to do for something that could be * called very frequently. */ var isNull = ReferenceEquals(argumentValue, null); if (isNull) { if (argumentValidation.HasFlag(ObjectIs.NotNull)) { var normalizedArgumentName = string.IsNullOrWhiteSpace(argumentName) ? Exceptions.Fragment_UnknownArgument : argumentName; throw new ArgumentNullException(normalizedArgumentName, string.Format(CultureInfo.CurrentCulture, Exceptions.Validate_ArgumentIsNull, normalizedArgumentName)); } return(Array.Empty <T>()); } var checkElements = elementValidation.HasFlag(ObjectIs.NotNull); var index = 0; var elements = new List <T>(); foreach (var element in argumentValue) { if (checkElements && ReferenceEquals(element, null)) { var normalizedArgumentName = string.Concat(string.IsNullOrWhiteSpace(argumentName) ? Exceptions.Fragment_UnknownArgument : argumentName, "[", index.ToString(@"D", CultureInfo.InvariantCulture), @"]"); throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Exceptions.Validate_ArgumentIsNull, normalizedArgumentName), normalizedArgumentName); } ++index; elements.Add(element); } Contract.EndContractBlock(); return(elements.ToArray()); }
public static T[] Validate <T>([AssertionCondition(AssertionConditionType.IS_NOT_NULL)][CanBeNull][ValidatedNotNull] this IEnumerable <T> values, [InvokerParameterName][NotNull] string argumentName, ObjectIs argumentValidationType, ElementIs elementValidationType) where T : class { if (ReferenceEquals(values, null)) { if ((argumentValidationType & ObjectIs.NotNull) == ObjectIs.NotNull) { throw new ArgumentNullException(argumentName, ErrorMessages.ArgumentNull(argumentName)); } return(new T[0]); } if ((elementValidationType & ElementIs.NotNull) != ElementIs.NotNull) { return(values.ToArray()); } var results = values.ToArray(); for (var n = 0; n < results.Length; ++n) { if (ReferenceEquals(results[n], null)) { throw new ArgumentException($@"Item {n} in '{argumentName}' is null.", argumentName); } } return(results); }
public static void Validate <T>([AssertionCondition(AssertionConditionType.IS_NOT_NULL)][NoEnumeration][ValidatedNotNull] this T argumentValue, [NotNull][InvokerParameterName] string argumentName, ObjectIs validation) where T : class { /* * We should test "validation", but that would be an expensive thing to do for something that could be * called very frequently. */ var isNull = ReferenceEquals(argumentValue, null); if (validation.HasFlag(ObjectIs.NotNull) && isNull) { var normalizedArgumentName = string.IsNullOrWhiteSpace(argumentName) ? Exceptions.Fragment_UnknownArgument : argumentName; throw new ArgumentNullException(normalizedArgumentName, string.Format(CultureInfo.CurrentCulture, Exceptions.Validate_ArgumentIsNull, normalizedArgumentName)); } Contract.EndContractBlock(); }