using System; using System.Collections.Generic; using System.Text; namespace Appointment.Entity.ValidateAttribute { /// /// 字符串长度校验特性 /// /// /// Add:yuangb 2019-3-5 /// 使用格式:实体属性前加[StringLength(50, MinLength = 2, ErrorMessage = "获取微信用户信息失败")] /// public class StringLengthAttribute : ValidateAttribute { /// /// 最小长度 /// public int MinLength { get; set; } /// /// 报错提示 /// public string ErrorMessage { get; set; } private int _maxLength; public StringLengthAttribute(int maxLength) { this._maxLength = maxLength; } public override string Validate(object oValue) { if (oValue == null || string.IsNullOrWhiteSpace(oValue.ToString()) || oValue.ToString().Length > this._maxLength || oValue.ToString().Length < this.MinLength) { return ErrorMessage; } else { return null; } } } }