2 using System.ComponentModel.DataAnnotations;
4 namespace Sage.
ES.S50.ValidationsAttribute
9 [AttributeUsage(AttributeTargets.Property|AttributeTargets.Field)]
46 private readonly
string _comparisonProperty;
58 _comparisonProperty = comparisonProperty;
59 _operatorType = operatorType;
63 #region Private Methods 64 protected override ValidationResult IsValid(
object value, ValidationContext validationContext)
72 ErrorMessage = ErrorMessageString;
74 var
property = validationContext.ObjectType.GetProperty(_comparisonProperty);
78 throw new ArgumentException(
"Comparison property with this name not found");
81 var comparisonValue =
property.GetValue(validationContext.ObjectInstance,null);
84 if (comparisonValue.GetType() == typeof(DateTime) && value == null)
87 return ValidationResult.Success;
91 if (comparisonValue.GetType() == typeof(IComparable))
93 throw new ArgumentException(
"Comparison property has not implemented IComparable interface");
96 if (value.GetType() == typeof(IComparable))
98 throw new ArgumentException(
"value has not implemented IComparable interface");
101 var currentValue = (IComparable)value;
103 if (!ReferenceEquals(value.GetType(), comparisonValue.GetType()))
105 throw new ArgumentException(
"The properties types must be the same");
107 bool validation =
false;
108 int retCompare = currentValue.CompareTo((IComparable)comparisonValue);
109 switch (_operatorType)
112 validation = retCompare < 0;
115 validation = retCompare <= 0;
118 validation = retCompare > 0;
121 validation = retCompare >= 0;
124 validation = retCompare == 0;
127 validation = retCompare != 0;
134 return new ValidationResult(ErrorMessage,
new string[] { validationContext.MemberName });
137 return ValidationResult.Success;
ValidaciĆ³n: Compara el valor con otra propiedad
OperatorType
Tipo de operador