整理代码

This commit is contained in:
GG Z
2026-02-20 15:31:44 +08:00
parent 94cf3f3266
commit 9f121cfc7f
149 changed files with 4063 additions and 6964 deletions

View 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}");
}
}

View File

@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;
using System.Globalization;
namespace ShrlAlgoToolkit.Mvvm.Attributes;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
public sealed class IsNumericAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
return double.TryParse(value.ToString(), out var _) ? ValidationResult.Success : new("输入内容不是数值。");
}
public override bool IsValid(object value)
{
return double.TryParse(value.ToString(), out var _);
}
public override string FormatErrorMessage(string name)
{
return string.Format(CultureInfo.CurrentCulture, base.ErrorMessageString, [name]);
}
}

View File

@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;
namespace ShrlAlgoToolkit.Mvvm.Attributes;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
public sealed class MaximumAttribute : ValidationAttribute
{
public MaximumAttribute(double maxValue = 0)
{
MaxValue = maxValue;
}
public double MaxValue { get; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
return double.TryParse(value.ToString(), out var d) && d <= MaxValue ? ValidationResult.Success : new ValidationResult($"输入值超出范围,最大值应为{MaxValue}");
}
}

View File

@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;
namespace ShrlAlgoToolkit.Mvvm.Attributes;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
public sealed class MinimumAttribute : ValidationAttribute
{
public MinimumAttribute(double minValue = 0)
{
MinValue = minValue;
}
public double MinValue { get; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
return double.TryParse(value.ToString(), out var d) && d >= MinValue ? ValidationResult.Success : new ValidationResult($"最小值应为{MinValue}");
}
}

View File

@@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;
namespace ShrlAlgoToolkit.Mvvm.Attributes;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
public sealed class NotNullOrEmptyAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var b = value != null || (value is string str && !string.IsNullOrEmpty(str));
return b ? ValidationResult.Success : new("输入内容不是数值。");
}
}

View File

@@ -0,0 +1,31 @@
using System.ComponentModel.DataAnnotations;
namespace ShrlAlgoToolkit.Mvvm.Attributes
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
public class UndefinedCharAttribute : ValidationAttribute
{
public UndefinedCharAttribute(params char[] chars)
{
Chars = chars;
}
public char[] Chars { get; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//char[] chars = new[] { '\\', ':', '{', '}', '[', ']', '|', ';', '<', '>', '?', '`', '~' };
if (value != null)
{
foreach (var c in Chars)
{
if (value.ToString().IndexOf(c) > -1)
{
return new ValidationResult($"输入字符不合法,名称不能包含字符:'\\ : [ ] | {{}}; < > ? ` ~");
}
}
}
return ValidationResult.Success;
}
}
}