You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.6 KiB

3 years ago
using System;
namespace learun.wechat
{
/// <summary>
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.11.06
/// 描 述:字段长度属性
/// </summary>
[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; }
/// <summary>
///
/// </summary>
/// <param name="minLength"></param>
/// <param name="maxLength"></param>
public LengthAttribute(int minLength, int maxLength)
{
MinLength = minLength;
MaxLength = maxLength;
Message = string.Format("字符串长度应在{0}到{1}之间", minLength, maxLength);
}
/// <summary>
///
/// </summary>
/// <param name="minLength"></param>
/// <param name="maxLength"></param>
/// <param name="message"></param>
public LengthAttribute(int minLength, int maxLength, string message)
{
MinLength = minLength;
MaxLength = maxLength;
Message = string.Format(message, minLength, maxLength);
}
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="obj"></param>
/// <param name="message"></param>
/// <returns></returns>
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;
}
}
}