KeywordDescriptionAttribute.cs 3.4 KB

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