11using System ;
2+ using System . Reflection ;
23
34namespace Skybrud . Essentials . Reflection . Extensions {
45
56 /// <summary>
67 /// Extension methods for working with reflection.
78 /// </summary>
8- public static class ReflectionExtensions {
9+ public static class ReflectionExtensions {
10+
11+ /// <summary>
12+ /// Returns whether the specified enum <paramref name="value"/> has an attribute of type <typeparamref name="T"/>.
13+ /// </summary>
14+ /// <typeparam name="T">The type of the attribute.</typeparam>
15+ /// <param name="value">The enum value.</param>
16+ /// <returns><c>true</c>c> if an attribute is found; otherwise <c>false</c>.</returns>
17+ public static bool HasCustomAttribute < T > ( this Enum value ) where T : Attribute {
18+ return ReflectionUtils . HasCustomAttribute < T > ( value ) ;
19+ }
920
1021 /// <summary>
1122 /// Returns whether the specified enum <paramref name="value"/> has an attribute of type <typeparamref name="T"/>.
@@ -15,7 +26,7 @@ public static class ReflectionExtensions {
1526 /// <param name="result">The first attribute of <typeparamref name="T"/>, or <c>null</c> if no matches.</param>
1627 /// <returns><c>true</c>c> if an attribute is found; otherwise <c>false</c>.</returns>
1728 public static bool HasCustomAttribute < T > ( this Enum value , out T result ) where T : Attribute {
18- return ReflectionUtils . HasCustomAttribute < T > ( value , out result ) ;
29+ return ReflectionUtils . HasCustomAttribute ( value , out result ) ;
1930 }
2031
2132 /// <summary>
@@ -26,7 +37,7 @@ public static bool HasCustomAttribute<T>(this Enum value, out T result) where T
2637 /// <param name="result">When this method returns, an array containing the matched attributes.</param>
2738 /// <returns><c>true</c>c> if one or more attributes are found; otherwise <c>false</c>.</returns>
2839 public static bool HasCustomAttributes < T > ( this Enum value , out T [ ] result ) where T : Attribute {
29- return ReflectionUtils . HasCustomAttributes < T > ( value , out result ) ;
40+ return ReflectionUtils . HasCustomAttributes ( value , out result ) ;
3041 }
3142
3243 /// <summary>
@@ -49,6 +60,148 @@ public static T[] GetCustomAttributes<T>(this Enum value) where T : Attribute {
4960 return ReflectionUtils . GetCustomAttributes < T > ( value ) ;
5061 }
5162
63+ /// <summary>
64+ /// Returns whether the specified <paramref name="member"/> has an attribute of type <typeparamref name="T"/>.
65+ /// </summary>
66+ /// <typeparam name="T">The type of the attribute.</typeparam>
67+ /// <param name="member">The member info.</param>
68+ /// <returns><c>true</c>c> if an attribute is found; otherwise <c>false</c>.</returns>
69+ public static bool HasCustomAttribute < T > ( this MemberInfo member ) where T : Attribute {
70+ return ReflectionUtils . HasCustomAttribute < T > ( member ) ;
71+ }
72+
73+ /// <summary>
74+ /// Returns whether the specified <paramref name="member"/> has an attribute of type <typeparamref name="T"/>.
75+ /// </summary>
76+ /// <typeparam name="T">The type of the attribute.</typeparam>
77+ /// <param name="member">The member info.</param>
78+ /// <param name="result">The first attribute of <typeparamref name="T"/>, or <c>null</c> if no matches.</param>
79+ /// <returns><c>true</c>c> if an attribute is found; otherwise <c>false</c>.</returns>
80+ public static bool HasCustomAttribute < T > ( this MemberInfo member , out T result ) where T : Attribute {
81+ return ReflectionUtils . HasCustomAttribute ( member , out result ) ;
82+ }
83+
84+ /// <summary>
85+ /// Returns whether the specified <paramref name="member"/> has one or more attributes of type <typeparamref name="T"/>.
86+ /// </summary>
87+ /// <typeparam name="T">The type of the attributes.</typeparam>
88+ /// <param name="member">The member info.</param>
89+ /// <param name="result">When this method returns, an array containing the matched attributes.</param>
90+ /// <returns><c>true</c>c> if one or more attributes are found; otherwise <c>false</c>.</returns>
91+ public static bool HasCustomAttributes < T > ( this MemberInfo member , out T [ ] result ) where T : Attribute {
92+ return ReflectionUtils . HasCustomAttributes ( member , out result ) ;
93+ }
94+
95+ /// <summary>
96+ /// Returns the first attribute of type <typeparamref name="T"/>, or <c>null</c> if no matching attributes are found.
97+ /// </summary>
98+ /// <typeparam name="T">The type of the attribute to return.</typeparam>
99+ /// <param name="member">The member holding the attribute.</param>
100+ /// <returns>An instance of <typeparamref name="T"/>, or <c>null</c> if no matching attributes are found.</returns>
101+ public static T GetCustomAttribute < T > ( this MemberInfo member ) where T : Attribute {
102+ return ReflectionUtils . GetCustomAttribute < T > ( member ) ;
103+ }
104+
105+ /// <summary>
106+ /// Returns an array of attributes of type <typeparamref name="T"/>.
107+ /// </summary>
108+ /// <typeparam name="T">The type of the attributes to return.</typeparam>
109+ /// <param name="member">The member holding the attributes.</param>
110+ /// <returns>An array of <typeparamref name="T"/>.</returns>
111+ public static T [ ] GetCustomAttributes < T > ( this MemberInfo member ) where T : Attribute {
112+ return ReflectionUtils . GetCustomAttributes < T > ( member ) ;
113+ }
114+
115+ /// <summary>
116+ /// Returns whether the specified <paramref name="type"/> has an attribute of type <typeparamref name="T"/>.
117+ /// </summary>
118+ /// <typeparam name="T">The type of the attribute.</typeparam>
119+ /// <param name="type">The type holding the attribute.</param>
120+ /// <returns><c>true</c>c> if an attribute is found; otherwise <c>false</c>.</returns>
121+ public static bool HasCustomAttribute < T > ( this Type type ) where T : Attribute {
122+ return ReflectionUtils . HasCustomAttribute < T > ( type ) ;
123+ }
124+
125+ /// <summary>
126+ /// Returns whether the specified <paramref name="type"/> has an attribute of type <typeparamref name="T"/>.
127+ /// </summary>
128+ /// <typeparam name="T">The type of the attribute.</typeparam>
129+ /// <param name="type">The type holding the attribute.</param>
130+ /// <param name="result">The first attribute of <typeparamref name="T"/>, or <c>null</c> if no matches.</param>
131+ /// <returns><c>true</c>c> if an attribute is found; otherwise <c>false</c>.</returns>
132+ public static bool HasCustomAttribute < T > ( this Type type , out T result ) where T : Attribute {
133+ return ReflectionUtils . HasCustomAttribute ( type , out result ) ;
134+ }
135+
136+ /// <summary>
137+ /// Returns whether the specified <paramref name="type"/> has one or more attributes of type <typeparamref name="T"/>.
138+ /// </summary>
139+ /// <typeparam name="T">The type of the attributes.</typeparam>
140+ /// <param name="type">The type holding the attributes.</param>
141+ /// <param name="result">When this method returns, an array containing the matched attributes.</param>
142+ /// <returns><c>true</c>c> if one or more attributes are found; otherwise <c>false</c>.</returns>
143+ public static bool HasCustomAttributes < T > ( this Type type , out T [ ] result ) where T : Attribute {
144+ return ReflectionUtils . HasCustomAttributes ( type , out result ) ;
145+ }
146+
147+ /// <summary>
148+ /// Returns the first attribute of type <typeparamref name="T"/>, or <c>null</c> if no matching attributes are found.
149+ /// </summary>
150+ /// <typeparam name="T">The type of the attribute to return.</typeparam>
151+ /// <param name="type">The type holding the attribute.</param>
152+ /// <returns>An instance of <typeparamref name="T"/>, or <c>null</c> if no matching attributes are found.</returns>
153+ public static T GetCustomAttribute < T > ( this Type type ) where T : Attribute {
154+ return ReflectionUtils . GetCustomAttribute < T > ( type ) ;
155+ }
156+
157+ /// <summary>
158+ /// Returns an array of attributes of type <typeparamref name="T"/>.
159+ /// </summary>
160+ /// <typeparam name="T">The type of the attributes to return.</typeparam>
161+ /// <param name="type">The type holding the attributes.</param>
162+ /// <returns>An array of <typeparamref name="T"/>.</returns>
163+ public static T [ ] GetCustomAttributes < T > ( this Type type ) where T : Attribute {
164+ return ReflectionUtils . GetCustomAttributes < T > ( type ) ;
165+ }
166+
167+ /// <summary>
168+ /// Returns whether the specified <paramref name="member" /> is marked as obsolete.
169+ /// </summary>
170+ /// <param name="member">The member.</param>
171+ /// <returns><c>true</c> if the member has been marked as obsolete; otherwise <c>false</c>.</returns>
172+ public static bool IsObsolete ( this MemberInfo member ) {
173+ return ReflectionUtils . IsObsolete ( member , out _ ) ;
174+ }
175+
176+ /// <summary>
177+ /// Returns whether the specified <paramref name="member" /> is marked as obsolete.
178+ /// </summary>
179+ /// <param name="member">The member.</param>
180+ /// <param name="attribute">An instance of <see cref="T:System.ObsoleteAttribute" /> if the member has been marked as obsolete.</param>
181+ /// <returns><c>true</c> if the member has been marked as obsolete; otherwise <c>false</c>.</returns>
182+ public static bool IsObsolete ( MemberInfo member , out ObsoleteAttribute attribute ) {
183+ return ReflectionUtils . IsObsolete ( member , out attribute ) ;
184+ }
185+
186+ /// <summary>
187+ /// Returns whether the specified <paramref name="type" /> is marked as obsolete.
188+ /// </summary>
189+ /// <param name="type">The type.</param>
190+ /// <returns><c>true</c> if the member has been marked as obsolete; otherwise <c>false</c>.</returns>
191+ public static bool IsObsolete ( this Type type ) {
192+ return ReflectionUtils . IsObsolete ( type , out _ ) ;
193+ }
194+
195+ /// <summary>
196+ /// Returns whether the specified <paramref name="type" /> is marked as obsolete.
197+ /// </summary>
198+ /// <param name="type">The type.</param>
199+ /// <param name="attribute">An instance of <see cref="T:System.ObsoleteAttribute" /> if the type has been marked as obsolete.</param>
200+ /// <returns><c>true</c> if the type has been marked as obsolete; otherwise <c>false</c>.</returns>
201+ public static bool IsObsolete ( Type type , out ObsoleteAttribute attribute ) {
202+ return ReflectionUtils . IsObsolete ( type , out attribute ) ;
203+ }
204+
52205 }
53206
54207}
0 commit comments