/// <summary> /// Gets the validation supplier which will provide the validation ranges dynamically. /// </summary> /// <returns>The instance returned by the static method named as the InstanceName</returns> /// <exception cref="ValidationException">An exception will be thrown if the type does not contain such a static method</exception> private IDynamicValidationValues GetValidationSupplier() { PropertyInfo propInfo = DynamicValidationValuesImplementation.GetProperty(InstanceName); if (!typeof(IDynamicValidationValues).IsAssignableFrom(propInfo.PropertyType)) { throw new ValidationException(string.Format("{0}.{1} needs to return an instance that implements {2}", DynamicValidationValuesImplementation.Name, InstanceName, typeof(IDynamicValidationValues).Name)); } IDynamicValidationValues retVal = propInfo.GetMethod.Invoke(null, null) as IDynamicValidationValues; return(retVal); }
/// <summary> /// Validates the specified value with respect to the current validation attribute. /// </summary> /// <returns>An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult" /> class. </returns> /// <param name="value">The value to validate.</param> /// <param name="validationContext">The context information about the validation operation.</param> protected override ValidationResult IsValid(object value, ValidationContext validationContext) { ValidationResult retVal = ValidationResult.Success; IDynamicValidationValues supplier = GetValidationSupplier(); if (value is byte) { byte byteValue = (byte)value; // Invoke the properties byte min = supplier.MinByte; byte max = supplier.MaxByte; if ((byteValue > max) || (byteValue < min)) { retVal = new ValidationResult(string.Format(ErrorMessage, min, max)); } } else if (value is int) { int intValue = (int)value; // Invoke the properties int min = supplier.MinInt; int max = supplier.MaxInt; if ((intValue > max) || (intValue < min)) { retVal = new ValidationResult(string.Format(ErrorMessage, min, max)); } } else if (value is double) { double doubleValue = (double)value; // Invoke the properties double min = supplier.MinDouble; double max = supplier.MaxDouble; if ((doubleValue > max) || (doubleValue < min)) { retVal = new ValidationResult(string.Format(ErrorMessage, min, max)); } } else if (value is string) { string stringValue = (string)value; // Invoke the properties string min = supplier.MinString; string max = supplier.MaxString; if ((stringValue.CompareTo(max) > 0) || (stringValue.CompareTo(min) < 0)) { retVal = new ValidationResult(string.Format(ErrorMessage, min, max)); } } else if (value is DateTime) { DateTime dateTimeValue = (DateTime)value; // Invoke the properties DateTime min = supplier.MinDateTime; DateTime max = supplier.MaxDateTime; if ((dateTimeValue > max) || (dateTimeValue < min)) { retVal = new ValidationResult(string.Format(ErrorMessage, min, max)); } } else { throw new ValidationException(string.Format("{0} does not currently support range validation for {1}", GetType().Name, value.GetType().Name)); } return(retVal); }