using System; namespace learun.wechat { /// /// 版 本 EasyCode EC管理后台 /// Copyright (c) 2019-present EC管理有限公司 /// 创建人:tobin /// 日 期:2019.11.06 /// 描 述:字段长度属性 /// [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)] public class LengthAttribute : Attribute, IVerifyAttribute { private int MinLength { get; set; } private int MaxLength { get; set; } private string Message { get; set; } /// /// /// /// /// public LengthAttribute(int minLength, int maxLength) { MinLength = minLength; MaxLength = maxLength; Message = string.Format("字符串长度应在{0}到{1}之间", minLength, maxLength); } /// /// /// /// /// /// public LengthAttribute(int minLength, int maxLength, string message) { MinLength = minLength; MaxLength = maxLength; Message = string.Format(message, minLength, maxLength); } /// /// /// /// /// /// /// public bool Verify(Type type, object obj, out string message) { message = ""; if (type == typeof(string) && obj != null) { if ((obj as string).Length > MaxLength || (obj as string).Length < MinLength) { message = Message; return false; } } return true; } } }