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 } } } } }
/// <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); }
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)); } } }
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); } } }
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); } } }
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) }); } }
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); } }
/// <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)); }
/// <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)); }