整理代码
This commit is contained in:
@@ -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}");
|
||||
}
|
||||
}
|
||||
21
ShrlAlgoToolkit.RevitAddins/Attributes/IsNumericAttribute.cs
Normal file
21
ShrlAlgoToolkit.RevitAddins/Attributes/IsNumericAttribute.cs
Normal 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]);
|
||||
}
|
||||
}
|
||||
19
ShrlAlgoToolkit.RevitAddins/Attributes/MaximumAttribute.cs
Normal file
19
ShrlAlgoToolkit.RevitAddins/Attributes/MaximumAttribute.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
19
ShrlAlgoToolkit.RevitAddins/Attributes/MinimumAttribute.cs
Normal file
19
ShrlAlgoToolkit.RevitAddins/Attributes/MinimumAttribute.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
@@ -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("输入内容不是数值。");
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user