示例#1
0
        /// <summary>
        /// Validates the parameter.
        /// </summary>
        /// <param name="invocation">The invocation.</param>
        /// <param name="parameterPosition">The parameter position.</param>
        /// <param name="runWhen">The run when.</param>
        /// <param name="errors">The errors.</param>
        protected virtual void ValidateParameter(IInvocation invocation, int parameterPosition, RunWhen runWhen, ErrorSummary errors)
        {
            MethodInfo method = invocation.Method;


            ParameterInfoMeta parameterInfoMeta;

            IValidator[] validators = methodValidatorMetaStore.GetValidatorsFor(method, parameterPosition, null, runWhen, out parameterInfoMeta);

            foreach (IValidator validator in validators)
            {
                IPropertyAccessAware propertyAccessAware = (IPropertyAccessAware)validator;
                object value = invocation.Arguments[parameterPosition];

                if (parameterInfoMeta == ParameterInfoMeta.ParamsArray)
                {
                    ValidateParamsArray(validator, propertyAccessAware, value, errors);
                    continue;
                }

                propertyAccessAware.PropertyAccessor = delegate { return(invocation.Arguments[parameterPosition]); };

                if (validator.IsValid(value))
                {
                    continue;
                }

                AppendError(validator, errors);
            }
        }
示例#2
0
 /// <summary>
 /// Validate method parameters that are decorated with the params keyword.
 /// </summary>
 /// <param name="validator">The validator.</param>
 /// <param name="propertyAccessAware">The property access aware.</param>
 /// <param name="value">The value.</param>
 /// <param name="errors">The errors.</param>
 private void ValidateParamsArray(IValidator validator, IPropertyAccessAware propertyAccessAware, object value, ErrorSummary errors)
 {
     object[] paramsValue = (object[])value;
     foreach (object paramValue in paramsValue)
     {
         propertyAccessAware.PropertyAccessor = delegate { return(paramValue); };
         if (validator.IsValid(paramValue))
         {
             continue;
         }
         AppendError(validator, errors);
     }
 }
		/// <summary>
		/// Validate method parameters that are decorated with the params keyword.
		/// </summary>
		/// <param name="validator">The validator.</param>
		/// <param name="propertyAccessAware">The property access aware.</param>
		/// <param name="value">The value.</param>
		/// <param name="errors">The errors.</param>
		private void ValidateParamsArray(IValidator validator, IPropertyAccessAware propertyAccessAware, object value, ErrorSummary errors)
		{
			object[] paramsValue = (object[]) value;
			foreach (object paramValue in paramsValue)
			{
				propertyAccessAware.PropertyAccessor = delegate { return paramValue; };
				if (validator.IsValid(paramValue)) continue;
				AppendError(validator, errors);
			}
		}