CustomAttributeExtensions.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Reflection
  6. {
  7. public static class CustomAttributeExtensions
  8. {
  9. #region APIs that return a single attribute
  10. public static Attribute? GetCustomAttribute(this Assembly element, Type attributeType)
  11. {
  12. return Attribute.GetCustomAttribute(element, attributeType);
  13. }
  14. public static Attribute? GetCustomAttribute(this Module element, Type attributeType)
  15. {
  16. return Attribute.GetCustomAttribute(element, attributeType);
  17. }
  18. public static Attribute? GetCustomAttribute(this MemberInfo element, Type attributeType)
  19. {
  20. return Attribute.GetCustomAttribute(element, attributeType);
  21. }
  22. public static Attribute? GetCustomAttribute(this ParameterInfo element, Type attributeType)
  23. {
  24. return Attribute.GetCustomAttribute(element, attributeType);
  25. }
  26. public static T? GetCustomAttribute<T>(this Assembly element) where T : Attribute
  27. {
  28. return (T?)GetCustomAttribute(element, typeof(T));
  29. }
  30. public static T? GetCustomAttribute<T>(this Module element) where T : Attribute
  31. {
  32. return (T?)GetCustomAttribute(element, typeof(T));
  33. }
  34. public static T? GetCustomAttribute<T>(this MemberInfo element) where T : Attribute
  35. {
  36. return (T?)GetCustomAttribute(element, typeof(T));
  37. }
  38. public static T? GetCustomAttribute<T>(this ParameterInfo element) where T : Attribute
  39. {
  40. return (T?)GetCustomAttribute(element, typeof(T));
  41. }
  42. public static Attribute? GetCustomAttribute(this MemberInfo element, Type attributeType, bool inherit)
  43. {
  44. return Attribute.GetCustomAttribute(element, attributeType, inherit);
  45. }
  46. public static Attribute? GetCustomAttribute(this ParameterInfo element, Type attributeType, bool inherit)
  47. {
  48. return Attribute.GetCustomAttribute(element, attributeType, inherit);
  49. }
  50. public static T? GetCustomAttribute<T>(this MemberInfo element, bool inherit) where T : Attribute
  51. {
  52. return (T?)GetCustomAttribute(element, typeof(T), inherit);
  53. }
  54. public static T? GetCustomAttribute<T>(this ParameterInfo element, bool inherit) where T : Attribute
  55. {
  56. return (T?)GetCustomAttribute(element, typeof(T), inherit);
  57. }
  58. #endregion
  59. #region APIs that return all attributes
  60. public static IEnumerable<Attribute> GetCustomAttributes(this Assembly element)
  61. {
  62. return Attribute.GetCustomAttributes(element);
  63. }
  64. public static IEnumerable<Attribute> GetCustomAttributes(this Module element)
  65. {
  66. return Attribute.GetCustomAttributes(element);
  67. }
  68. public static IEnumerable<Attribute> GetCustomAttributes(this MemberInfo element)
  69. {
  70. return Attribute.GetCustomAttributes(element);
  71. }
  72. public static IEnumerable<Attribute> GetCustomAttributes(this ParameterInfo element)
  73. {
  74. return Attribute.GetCustomAttributes(element);
  75. }
  76. public static IEnumerable<Attribute> GetCustomAttributes(this MemberInfo element, bool inherit)
  77. {
  78. return Attribute.GetCustomAttributes(element, inherit);
  79. }
  80. public static IEnumerable<Attribute> GetCustomAttributes(this ParameterInfo element, bool inherit)
  81. {
  82. return Attribute.GetCustomAttributes(element, inherit);
  83. }
  84. #endregion
  85. #region APIs that return all attributes of a particular type
  86. public static IEnumerable<Attribute> GetCustomAttributes(this Assembly element, Type attributeType)
  87. {
  88. return Attribute.GetCustomAttributes(element, attributeType);
  89. }
  90. public static IEnumerable<Attribute> GetCustomAttributes(this Module element, Type attributeType)
  91. {
  92. return Attribute.GetCustomAttributes(element, attributeType);
  93. }
  94. public static IEnumerable<Attribute> GetCustomAttributes(this MemberInfo element, Type attributeType)
  95. {
  96. return Attribute.GetCustomAttributes(element, attributeType);
  97. }
  98. public static IEnumerable<Attribute> GetCustomAttributes(this ParameterInfo element, Type attributeType)
  99. {
  100. return Attribute.GetCustomAttributes(element, attributeType);
  101. }
  102. public static IEnumerable<T> GetCustomAttributes<T>(this Assembly element) where T : Attribute
  103. {
  104. return (IEnumerable<T>)GetCustomAttributes(element, typeof(T));
  105. }
  106. public static IEnumerable<T> GetCustomAttributes<T>(this Module element) where T : Attribute
  107. {
  108. return (IEnumerable<T>)GetCustomAttributes(element, typeof(T));
  109. }
  110. public static IEnumerable<T> GetCustomAttributes<T>(this MemberInfo element) where T : Attribute
  111. {
  112. return (IEnumerable<T>)GetCustomAttributes(element, typeof(T));
  113. }
  114. public static IEnumerable<T> GetCustomAttributes<T>(this ParameterInfo element) where T : Attribute
  115. {
  116. return (IEnumerable<T>)GetCustomAttributes(element, typeof(T));
  117. }
  118. public static IEnumerable<Attribute> GetCustomAttributes(this MemberInfo element, Type attributeType, bool inherit)
  119. {
  120. return Attribute.GetCustomAttributes(element, attributeType, inherit);
  121. }
  122. public static IEnumerable<Attribute> GetCustomAttributes(this ParameterInfo element, Type attributeType, bool inherit)
  123. {
  124. return Attribute.GetCustomAttributes(element, attributeType, inherit);
  125. }
  126. public static IEnumerable<T> GetCustomAttributes<T>(this MemberInfo element, bool inherit) where T : Attribute
  127. {
  128. return (IEnumerable<T>)GetCustomAttributes(element, typeof(T), inherit);
  129. }
  130. public static IEnumerable<T> GetCustomAttributes<T>(this ParameterInfo element, bool inherit) where T : Attribute
  131. {
  132. return (IEnumerable<T>)GetCustomAttributes(element, typeof(T), inherit);
  133. }
  134. #endregion
  135. #region IsDefined
  136. public static bool IsDefined(this Assembly element, Type attributeType)
  137. {
  138. return Attribute.IsDefined(element, attributeType);
  139. }
  140. public static bool IsDefined(this Module element, Type attributeType)
  141. {
  142. return Attribute.IsDefined(element, attributeType);
  143. }
  144. public static bool IsDefined(this MemberInfo element, Type attributeType)
  145. {
  146. return Attribute.IsDefined(element, attributeType);
  147. }
  148. public static bool IsDefined(this ParameterInfo element, Type attributeType)
  149. {
  150. return Attribute.IsDefined(element, attributeType);
  151. }
  152. public static bool IsDefined(this MemberInfo element, Type attributeType, bool inherit)
  153. {
  154. return Attribute.IsDefined(element, attributeType, inherit);
  155. }
  156. public static bool IsDefined(this ParameterInfo element, Type attributeType, bool inherit)
  157. {
  158. return Attribute.IsDefined(element, attributeType, inherit);
  159. }
  160. #endregion
  161. }
  162. }