Пример #1
0
        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());
        }
Пример #2
0
        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();
        }