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.
 
 
 
 
 
 

44 lines
1.2 KiB

  1. using System;
  2. namespace Learun.Application.WeChat
  3. {
  4. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field , AllowMultiple = false, Inherited = false)]
  5. public class LengthAttribute : System.Attribute, IVerifyAttribute
  6. {
  7. int MinLength { get; set; }
  8. int MaxLength { get; set; }
  9. string Message { get; set; }
  10. public LengthAttribute(int minLength, int maxLength)
  11. {
  12. MinLength = minLength;
  13. MaxLength = maxLength;
  14. Message = string.Format("字符串长度应在{0}到{1}之间",minLength,maxLength);
  15. }
  16. public LengthAttribute(int minLength, int maxLength,string message)
  17. {
  18. MinLength = minLength;
  19. MaxLength = maxLength;
  20. Message = string.Format(message, minLength, maxLength);
  21. }
  22. public bool Verify(Type type,object obj,out string message)
  23. {
  24. message = "";
  25. if (type == typeof(string) && obj != null)
  26. {
  27. if ((obj as string).Length > MaxLength || (obj as string).Length < MinLength)
  28. {
  29. message = Message;
  30. return false;
  31. }
  32. }
  33. return true;
  34. }
  35. }
  36. }