更新整理
This commit is contained in:
22
ShrlAlgoToolkit.Mvvm/Attributes/GreaterThanAttribute.cs
Normal file
22
ShrlAlgoToolkit.Mvvm/Attributes/GreaterThanAttribute.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Attributes;
|
||||
|
||||
[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}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user