KeywordDescriptionAttribute.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Wayne.Lib.StateEngine
  5. {
  6. /// <summary>
  7. /// Add a keyword to the state. Group the keywords in categories.
  8. /// </summary>
  9. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
  10. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1036:OverrideMethodsOnComparableTypes")]
  11. public sealed class KeywordDescriptionAttribute : Attribute, IComparable
  12. {
  13. #region Fields
  14. private object category;
  15. private object keyword;
  16. private string description;
  17. #endregion
  18. #region Construction
  19. /// <summary>
  20. /// Add a keyword to the state. Group the keywords in categories.
  21. /// </summary>
  22. /// <param name="category">Category that the keyword belongs to.</param>
  23. /// <param name="keyword">Keyword that should be associated with the state.</param>
  24. /// <param name="description">Descriptive text.</param>
  25. public KeywordDescriptionAttribute(object category, object keyword, string description)
  26. {
  27. this.category = category;
  28. this.keyword = keyword;
  29. this.description = description;
  30. }
  31. /// <summary>
  32. /// Add a keyword to the state. Group the keywords in categories.
  33. /// </summary>
  34. /// <param name="category">Category that the keyword belongs to.</param>
  35. /// <param name="keyword">Keyword that should be associated with the state.</param>
  36. public KeywordDescriptionAttribute(object category, object keyword)
  37. : this(category, keyword, "")
  38. {
  39. }
  40. #endregion
  41. #region Properties
  42. /// <summary>
  43. /// Category that the keyword belongs to.
  44. /// </summary>
  45. public object Category
  46. {
  47. get { return category; }
  48. }
  49. /// <summary>
  50. /// Keyword that should be associated with the state.
  51. /// </summary>
  52. public object Keyword
  53. {
  54. get { return keyword; }
  55. }
  56. /// <summary>
  57. /// Keyword as a string.
  58. /// </summary>
  59. public string KeywordText
  60. {
  61. get
  62. {
  63. Type keywordAsType = keyword as Type;
  64. if (keywordAsType != null)
  65. return keywordAsType.FullName;
  66. return keyword.ToString();
  67. }
  68. }
  69. /// <summary>
  70. /// Descriptive text.
  71. /// </summary>
  72. public string Description
  73. {
  74. get { return description; }
  75. }
  76. #endregion
  77. #region IComparable Members
  78. /// <summary>
  79. /// Compares this attribute with another one.
  80. /// </summary>
  81. /// <param name="obj"></param>
  82. /// <returns></returns>
  83. public int CompareTo(object obj)
  84. {
  85. int result = 0;
  86. KeywordDescriptionAttribute compareKeywordDescriptionAttribute = obj as KeywordDescriptionAttribute;
  87. if (compareKeywordDescriptionAttribute != null)
  88. {
  89. result = category.ToString().CompareTo(compareKeywordDescriptionAttribute.category.ToString());
  90. if (result == 0)
  91. result = KeywordText.CompareTo(compareKeywordDescriptionAttribute.KeywordText);
  92. }
  93. return result;
  94. }
  95. #endregion
  96. }
  97. }