示例#1
0
        IEnumerable <MethodInfo> IValidatedType.GetSelfValidationMethods()
        {
            Type type = this.TargetType;

            if (ValidationReflectionHelper.GetCustomAttributes(type, typeof(HasSelfValidationAttribute), false).Length == 0)
            {
                yield break;            // no self validation for the current type, ignore type
            }
            foreach (MethodInfo methodInfo in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                bool            hasReturnType = methodInfo.ReturnType != typeof(void);
                ParameterInfo[] parameters    = methodInfo.GetParameters();

                if (!hasReturnType && parameters.Length == 1 && parameters[0].ParameterType == typeof(ValidationResults))
                {
                    foreach (SelfValidationAttribute attribute
                             in ValidationReflectionHelper.GetCustomAttributes(methodInfo, typeof(SelfValidationAttribute), false))
                    {
                        if (this.Ruleset.Equals(attribute.Ruleset))
                        {
                            yield return(methodInfo);

                            continue;   // done with current method
                        }
                    }
                }
            }
        }
示例#2
0
 /// <summary>
 /// Updates the flyweight for a parameter.
 /// </summary>
 /// <param name="parameterInfo">The parameter.</param>
 public void UpdateFlyweight(ParameterInfo parameterInfo)
 {
     this.parameterInfo = parameterInfo;
     this.ignoreNullsAttribute
         = ValidationReflectionHelper.ExtractValidationAttribute <IgnoreNullsAttribute>(parameterInfo, string.Empty);
     this.validatorCompositionAttribute
         = ValidationReflectionHelper.ExtractValidationAttribute <ValidatorCompositionAttribute>(parameterInfo, string.Empty);
 }
示例#3
0
 private void UpdateFlyweight(MemberInfo memberInfo, Type targetType)
 {
     this.memberInfo = memberInfo;
     this.targetType = targetType;
     this.ignoreNullsAttribute
         = ValidationReflectionHelper.ExtractValidationAttribute <IgnoreNullsAttribute>(memberInfo, this.ruleset);
     this.validatorCompositionAttribute
         = ValidationReflectionHelper.ExtractValidationAttribute <ValidatorCompositionAttribute>(memberInfo, this.ruleset);
 }
 IEnumerable <IValidatedElement> IValidatedType.GetValidatedFields()
 {
     foreach (FieldInfo fieldInfo in
              ((IValidatedElement)this).TargetType.GetFields(BindingFlags.Public | BindingFlags.Instance))
     {
         if (ValidationReflectionHelper.IsValidField(fieldInfo))
         {
             yield return(new ValidationAttributeValidatedElement(fieldInfo));
         }
     }
 }
 IEnumerable <IValidatedElement> IValidatedType.GetValidatedProperties()
 {
     foreach (PropertyInfo propertyInfo in
              ((IValidatedElement)this).TargetType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
     {
         if (ValidationReflectionHelper.IsValidProperty(propertyInfo))
         {
             yield return(new ValidationAttributeValidatedElement(propertyInfo));
         }
     }
 }
示例#6
0
        IEnumerable <IValidatedElement> IValidatedType.GetValidatedProperties()
        {
            MetadataValidatedElement flyweight = new MetadataValidatedElement(this.Ruleset);

            foreach (PropertyInfo propertyInfo in this.TargetType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (ValidationReflectionHelper.IsValidProperty(propertyInfo))
                {
                    flyweight.UpdateFlyweight(propertyInfo);
                    yield return(flyweight);
                }
            }
        }
示例#7
0
        IEnumerable <IValidatedElement> IValidatedType.GetValidatedMethods()
        {
            MetadataValidatedElement flyweight = new MetadataValidatedElement(this.Ruleset);

            foreach (MethodInfo methodInfo in this.TargetType.GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {
                ParameterInfo[] parameters = methodInfo.GetParameters();

                if (ValidationReflectionHelper.IsValidMethod(methodInfo))
                {
                    flyweight.UpdateFlyweight(methodInfo);
                    yield return(flyweight);
                }
            }
        }
示例#8
0
        IEnumerable <IValidatorDescriptor> IValidatedElement.GetValidatorDescriptors()
        {
            if (this.memberInfo == null)
            {
                yield break;
            }

            foreach (ValidatorAttribute attribute in
                     ValidationReflectionHelper.GetCustomAttributes(this.memberInfo, typeof(ValidatorAttribute), false))
            {
                if (this.ruleset.Equals(attribute.Ruleset))
                {
                    yield return(attribute);
                }
            }
        }
        IEnumerable <IValidatorDescriptor> IValidatedElement.GetValidatorDescriptors()
        {
            var attributes =
                ValidationReflectionHelper
                .GetCustomAttributes(this.memberInfo, typeof(ValidationAttribute), true)
                .Cast <ValidationAttribute>()
                .Where(a => !typeof(BaseValidationAttribute).IsAssignableFrom(a.GetType()))
                .ToArray();

            if (attributes.Length == 0)
            {
                return(new IValidatorDescriptor[0]);
            }
            else
            {
                return(new IValidatorDescriptor[]
                {
                    new ValidationAttributeValidatorDescriptor(attributes)
                });
            }
        }
示例#10
0
        IEnumerable <IValidatedElement> IValidatedType.GetValidatedMethods()
        {
            ConfigurationValidatedElement flyweight = new ConfigurationValidatedElement();

            foreach (ValidatedMethodReference validatedMemberReference in this.ruleData.Methods)
            {
                if (validatedMemberReference.Validators.Count == 0)
                {
                    continue;
                }

                MethodInfo methodInfo
                    = ValidationReflectionHelper.GetMethod(this.targetType, validatedMemberReference.Name, false);
                if (methodInfo == null)
                {
                    continue;
                }

                flyweight.UpdateFlyweight(validatedMemberReference, methodInfo);
                yield return(flyweight);
            }
        }
示例#11
0
        /// <summary>
        /// This member supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
        /// </summary>
        public Validator CreateValidatorForMethod(Type type, ValidatedMethodReference methodReference)
        {
            if (methodReference == null)
            {
                throw new ArgumentNullException("methodReference");
            }

            if (methodReference.Validators.Count == 0)
            {
                return(null);
            }

            MethodInfo methodInfo = ValidationReflectionHelper.GetMethod(type, methodReference.Name, false);

            if (methodInfo == null)
            {
                return(null);
            }

            ConfigurationValidatedElement validatedElement = new ConfigurationValidatedElement(methodReference, methodInfo);

            return(CreateValidatorForValidatedElement(validatedElement, this.GetCompositeValidatorBuilderForMethod));
        }
示例#12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <param name="propertyReference"></param>
        /// <returns></returns>
        public Validator CreateValidatorForProperty(Type type, ValidatedPropertyReference propertyReference)
        {
            if (propertyReference == null)
            {
                throw new ArgumentNullException("propertyReference");
            }

            if (propertyReference.Validators.Count == 0)
            {
                return(null);
            }

            PropertyInfo propertyInfo = ValidationReflectionHelper.GetProperty(type, propertyReference.Name, false);

            if (propertyInfo == null)
            {
                return(null);
            }

            ConfigurationValidatedElement validatedElement = new ConfigurationValidatedElement(propertyReference, propertyInfo);

            return(CreateValidatorForValidatedElement(validatedElement, this.GetCompositeValidatorBuilderForProperty));
        }