Files
ShrlAlgoToolkit/ShrlAlgo.Toolkit.Mvvm/Attributes/GreaterThanAttribute.cs

23 lines
740 B
C#
Raw Normal View History

using System.ComponentModel.DataAnnotations;
2024-09-22 11:05:41 +08:00
namespace ShrlAlgo.Toolkit.Mvvm.Attributes;
2024-09-22 11:05:41 +08:00
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
public sealed class GreaterThanAttribute : ValidationAttribute
{
public GreaterThanAttribute(string propertyName)
{
PropertyName = propertyName;
}
public string PropertyName { get; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
object instance = validationContext.ObjectInstance,
otherValue = instance.GetType().GetProperty(PropertyName).GetValue(instance);
return ((IComparable)value).CompareTo(otherValue) > 0 ? ValidationResult.Success : new($"当前值小于{value}");
}
}