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.

76 lines
2.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Apewer.Web
  5. {
  6. /// <summary></summary>
  7. [Serializable]
  8. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  9. public class ApiAttribute : Attribute
  10. {
  11. private const bool DefaultVisible = true;
  12. private string _name = "";
  13. private string _caption = "";
  14. private bool _visible = DefaultVisible;
  15. /// <summary></summary>
  16. public string Name { get { return _name; } }
  17. /// <summary></summary>
  18. public string Caption { get { return _caption; } }
  19. /// <summary></summary>
  20. public bool Visible { get { return _visible; } }
  21. private void Initialize(string name = "", string caption = "", bool visible = DefaultVisible)
  22. {
  23. _name = string.IsNullOrEmpty(name) ? "" : name.ToLower().Trim();
  24. _caption = string.IsNullOrEmpty(caption) ? "" : caption.Trim();
  25. _visible = visible;
  26. }
  27. /// <summary></summary>
  28. public ApiAttribute(string name = null, string caption = null, bool visible = DefaultVisible)
  29. {
  30. Initialize(name, caption, visible);
  31. }
  32. /// <summary></summary>
  33. public static string GetName(Type type, bool inherit = true)
  34. {
  35. if (type == null) return null;
  36. var name = null as string;
  37. var attributes = type.GetCustomAttributes(typeof(ApiAttribute), inherit);
  38. if (attributes.Length > 0)
  39. {
  40. var attribute = attributes[0] as ApiAttribute;
  41. name = attribute.Name;
  42. }
  43. if (string.IsNullOrEmpty(name)) name = type.Name;
  44. return name;
  45. }
  46. /// <summary></summary>
  47. public static string GetCaption(Type type, bool inherit = true)
  48. {
  49. if (type == null) return null;
  50. var attributes = type.GetCustomAttributes(typeof(ApiAttribute), inherit);
  51. if (attributes.Length > 0)
  52. {
  53. var attribute = attributes[0] as ApiAttribute;
  54. return attribute.Caption;
  55. }
  56. return type.Name;
  57. }
  58. }
  59. }