/// <summary> /// Validates acceptable range of values for an object /// </summary> /// <param name="object"></param> /// <returns></returns> public override bool Validate(ValidationObject @object) { try { string value = GetPropertyValue(@object).ToString(); switch (DataType) { case ValidationDataType.Integer: int imin = int.Parse(Min.ToString()); int imax = int.Parse(Max.ToString()); int ival = int.Parse(value); return(ival >= imin && ival <= imax); case ValidationDataType.Double: double dmin = double.Parse(Min.ToString()); double dmax = double.Parse(Max.ToString()); double dval = double.Parse(value); return(dval >= dmin && dval <= dmax); case ValidationDataType.Decimal: decimal cmin = decimal.Parse(Min.ToString()); decimal cmax = decimal.Parse(Max.ToString()); decimal cval = decimal.Parse(value); return(cval >= cmin && cval <= cmax); case ValidationDataType.Date: DateTime tmin = DateTime.Parse(Min.ToString()); DateTime tmax = DateTime.Parse(Max.ToString()); DateTime tval = DateTime.Parse(value); return(tval >= tmin && tval <= tmax); case ValidationDataType.String: string smin = Min.ToString(); string smax = Max.ToString(); int result1 = string.Compare(smin, value); int result2 = string.Compare(value, smax); return(result1 >= 0 && result2 <= 0); } return(false); } catch { return(false); } }
/// <summary> /// Gets value for given validation object's property using reflection /// </summary> /// <param name="validationObject"></param> /// <returns></returns> protected object GetPropertyValue(ValidationObject validationObject) { // note: reflection is relatively slow return(validationObject.GetType().GetProperty(Property).GetValue(validationObject, null)); }
/// <summary> /// Implements validation method in derived classes. /// </summary> /// <param name="validationObject">Object to be validated</param> /// <returns>True if rules rules satisfied, false if not.</returns> public abstract bool Validate(ValidationObject validationObject);