/// <summary> /// Private method that compares a property value with a specified value. /// </summary> /// <typeparam name="T">Datatype of the property to validate.</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> /// <param name="compareType"><see cref="CompareType"/> defining the type of comparison that will be made.</param> /// <returns></returns> private static bool CompareValues <T>(object target, CompareValueRuleArgs <T> e, CompareType compareType) { bool result = true; if (e != null) { T compareValue = e.CompareValue; PropertyInfo p = target.GetType().GetProperty(e.PropertyName); T value; //if (p.PropertyType.Name.Equals(typeof(Nullable<>).Name)) //{ //} try { value = (T)p.GetValue(target, null); } catch (Exception) { return(true); } // if the property is read from a nullable type, then a null valid is considered as allowed if (p.PropertyType.Name.Equals(typeof(Nullable <>).Name) && value == null) { return(true); } int res = Comparer.DefaultInvariant.Compare(value, compareValue); switch (compareType) { case CompareType.LessThanOrEqualTo: result = (res <= 0); if (!result) { if (string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} cannot exceed {1}.", e.FriendlyName, compareValue.ToString()); } } break; case CompareType.LessThan: result = (res < 0); if (!result) { if (string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} must be less than {1}.", e.FriendlyName, compareValue.ToString()); } } break; case CompareType.EqualTo: result = (res == 0); if (!result) { if (string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} must equal {1}.", e.FriendlyName, compareValue.ToString()); } } break; case CompareType.GreaterThan: result = (res > 0); if (!result) { if (string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} must exceed {1}.", e.FriendlyName, compareValue.ToString()); } } break; case CompareType.GreaterThanOrEqualTo: result = (res >= 0); if (!result) { if (string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} must be greater than or equal to {1}.", e.FriendlyName, compareValue.ToString()); } } break; } if (!result) { } } return(result); }
private static bool CompareValues <T>(object target, CompareValueRuleArgs <T> e, CompareType compareType) { bool flag = true; if (e != null) { T a; T b = e.CompareValue; PropertyInfo property = target.GetType().GetProperty(e.PropertyName); try { a = (T)property.GetValue(target, null); } catch (Exception) { return(true); } if (property.PropertyType.Name.Equals(typeof(Nullable <>).Name) && (a == null)) { return(true); } int num = Comparer.DefaultInvariant.Compare(a, b); switch (compareType) { case CompareType.LessThanOrEqualTo: flag = num <= 0; if (!flag && string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} can not exceed {1}", e.PropertyName, b.ToString()); } break; case CompareType.LessThan: flag = num < 0; if (!flag && string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} must be less than {1}", e.PropertyName, b.ToString()); } break; case CompareType.EqualTo: flag = num == 0; if (!flag && string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} must equal {1}", e.PropertyName, b.ToString()); } break; case CompareType.GreaterThan: flag = num > 0; if (!flag && string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} must exceed {1}", e.PropertyName, b.ToString()); } break; case CompareType.GreaterThanOrEqualTo: flag = num >= 0; if (!flag && string.IsNullOrEmpty(e.Description)) { e.Description = string.Format("{0} must be greater than or equal to {1}", e.PropertyName, b.ToString()); } break; } if (!flag) { } } return(flag); }