自定义StringByteLength Attribute
using HKElectric.ESafety.Utilities; using System.ComponentModel.DataAnnotations; namespace Test { public class StringByteLengthAttribute : ValidationAttribute { public int MaximumLength { get; set; } public StringByteLengthAttribute(int maximumLength) { MaximumLength = maximumLength; } public override string FormatErrorMessage(string name) { return string.Format(CustomeMessage.SAFETYDOCUMENT_BYTE_VERIFY, name, MaximumLength); } public override bool IsValid(object? value) { string? context = value != null ? value.ToString() : string.Empty; return !string.IsNullOrWhiteSpace(context) ? StringUtil.CountByteLength(context, MaximumLength) : true; } } }