IsNotNull() public static method

Validates a parameter value is not null.
public static IsNotNull ( string paramName, object value ) : void
paramName string The parameter name.
value object The parameter value.
return void
Esempio n. 1
0
        /// <summary>
        /// Recursively validates <paramref name="instance"/>.
        /// </summary>
        /// <param name="instance">The object to validate.</param>
        /// <param name="results">A collection to hold each failed validation.</param>
        /// <param name="validateAllProperties">true to validate all properties; if false, only required attributes are validated.</param>
        /// <param name="prefix">The prefix to append to the field name when validation fails.</param>
        /// <returns>true if the object validates; otherwise, false.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="instance"/> is null.</exception>
        private static bool TryValidateObject(object instance, ICollection <ValidationResult> results, bool validateAllProperties, string prefix)
        {
            GuardClauses.IsNotNull(nameof(instance), instance);

            var tempResults = new List <ValidationResult>();

            ValidationContext validationContext = new ValidationContext(instance);
            var isValid = Validator.TryValidateObject(instance, validationContext, tempResults, validateAllProperties: validateAllProperties);

            foreach (var item in tempResults)
            {
                IEnumerable <string> memberNames = item.MemberNames.Select(name => (!string.IsNullOrEmpty(prefix) ? prefix + "." : string.Empty) + name);
                results.Add(new ValidationResult(item.ErrorMessage, memberNames));
            }

            foreach (var prop in instance.GetType().GetProperties())
            {
                if (prop.PropertyType != typeof(string))
                {
                    var value = prop.GetValue(instance);
                    if (value == null)
                    {
                        continue;
                    }
                    else if (value is IEnumerable <object> list)
                    {
                        var memberPrefix = (!string.IsNullOrEmpty(prefix) ? prefix + "." : string.Empty) + prop.Name;
                        int i            = 0;
                        foreach (var item in list)
                        {
                            if (!TryValidateObject(item, results, validateAllProperties, $"{memberPrefix}[{i}]"))
                            {
                                isValid = false;
                            }

                            i++;
                        }
                    }
                    else
                    {
                        var memberPrefix = (!string.IsNullOrEmpty(prefix) ? prefix + "." : string.Empty) + prop.Name;
                        if (!TryValidateObject(value, results, validateAllProperties, memberPrefix))
                        {
                            isValid = false;
                        }
                    }
                }
            }

            return(isValid);
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConditionalMiddleware"/> class.
        /// </summary>
        /// <param name="pipeline">The pipeline. Required.</param>
        /// <param name="condition">The condition to evaluate. Required.</param>
        /// <param name="configure">Configures the branch. Optional.</param>
        /// <param name="rejoinPipeline">Determines if the branch should rejoin the main pipeline or not.</param>
        public ConditionalMiddleware(
            IMiddlewarePipeline pipeline,
            Func <HttpContext, bool> condition,
            Action <IMiddlewarePipeline> configure,
            bool rejoinPipeline)
        {
            GuardClauses.IsNotNull(nameof(pipeline), pipeline);
            GuardClauses.IsNotNull(nameof(condition), condition);

            this.pipeline       = pipeline;
            this.configure      = configure;
            this.condition      = condition;
            this.rejoinPipeline = rejoinPipeline;
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MiddlewarePipeline"/> class.
 /// </summary>
 /// <param name="httpContextAccessor">The HTTP context accessor.</param>
 public MiddlewarePipeline(IHttpContextAccessor httpContextAccessor)
 {
     GuardClauses.IsNotNull(nameof(httpContextAccessor), httpContextAccessor);
     this.httpContextAccessor = httpContextAccessor;
 }
Esempio n. 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FunctionMiddleware"/> class.
 /// </summary>
 /// <param name="func">The task to be executed.</param>
 public FunctionMiddleware(Func <HttpContext, Task <IActionResult> > func)
 {
     GuardClauses.IsNotNull(nameof(func), func);
     this.func = func;
 }