/// <summary> /// Generic rule that determines if an object's property is within a specified range. /// </summary> /// <typeparam name="T">Datatype of the property to validate. Must implement <see cref="System.IComparable{T}"/>.</typeparam> /// <param name="target">Object containing the data to validate.</param> /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param> /// <returns>False if the rule is broken; true otherwise.</returns> public static bool InRange <T>(object target, ValidationRuleArgs e) { bool result = true; RangeRuleArgs <T> ruleArgs = e as RangeRuleArgs <T>; if (ruleArgs != null) { PropertyInfo p = target.GetType().GetProperty(e.PropertyName); T value = (T)p.GetValue(target, null); result = ruleArgs.Range.Contains(value); if (!result) { if (string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} must be between {1} and {2}.", ruleArgs.FriendlyName, ruleArgs.Range.MinValue, ruleArgs.Range.MaxValue); } } return(result); } else { throw new ArgumentException("Must be of type RangeRuleArgs.", "e"); } }
public static bool InRange <T>(object target, ValidationRuleArgs e) { bool flag = true; RangeRuleArgs <T> args = e as RangeRuleArgs <T>; if (args == null) { throw new ArgumentException("Must be of type RangeRuleArgs.", "e"); } T local = (T)target.GetType().GetProperty(e.PropertyName).GetValue(target, null); flag = args.Range.Contains(local); if (!flag && string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} must be between {1} and {2}.", args.PropertyName, args.Range.MinValue, args.Range.MaxValue); } return(flag); }