ValueValidatorBase.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Abp.Collections.Extensions;
  6. namespace Abp.Runtime.Validation
  7. {
  8. [Serializable]
  9. public abstract class ValueValidatorBase : IValueValidator
  10. {
  11. public virtual string Name
  12. {
  13. get
  14. {
  15. var type = GetType().GetTypeInfo();
  16. if (type.IsDefined(typeof(ValidatorAttribute)))
  17. {
  18. return type.GetCustomAttributes(typeof(ValidatorAttribute)).Cast<ValidatorAttribute>().First().Name;
  19. }
  20. return type.Name;
  21. }
  22. }
  23. /// <summary>
  24. /// Gets/sets arbitrary objects related to this object.
  25. /// Gets null if given key does not exists.
  26. /// </summary>
  27. /// <param name="key">Key</param>
  28. public object this[string key]
  29. {
  30. get { return Attributes.GetOrDefault(key); }
  31. set { Attributes[key] = value; }
  32. }
  33. /// <summary>
  34. /// Arbitrary objects related to this object.
  35. /// </summary>
  36. public IDictionary<string, object> Attributes { get; private set; }
  37. public abstract bool IsValid(object value);
  38. protected ValueValidatorBase()
  39. {
  40. Attributes = new Dictionary<string, object>();
  41. }
  42. }
  43. }