Skip to content

Commit 18c9c0c

Browse files
committed
Added more method overloads for ReflectionUtils and ReflectionExtensions
1 parent 50deea5 commit 18c9c0c

File tree

2 files changed

+258
-5
lines changed

2 files changed

+258
-5
lines changed

src/Skybrud.Essentials/Reflection/Extensions/ReflectionExtensions.cs

Lines changed: 156 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
using System;
2+
using System.Reflection;
23

34
namespace 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
}

src/Skybrud.Essentials/Reflection/ReflectionUtils.cs

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ public static bool IsObsolete(MemberInfo member, out ObsoleteAttribute attribute
128128
return attribute != null;
129129
}
130130

131+
/// <summary>
132+
/// Returns whether <paramref name="type"/> is marked as obsolete.
133+
/// </summary>
134+
/// <param name="type">The type to check.</param>
135+
/// <returns><c>true</c> if the member has been marked as obsolete; otherwise <c>false</c>.</returns>
136+
public static bool IsObsolete(Type type) {
137+
return IsObsolete(type, out _);
138+
}
139+
140+
/// <summary>
141+
/// Returns whether <paramref name="type"/> is marked as obsolete.
142+
/// </summary>
143+
/// <param name="type">The type to check.</param>
144+
/// <param name="attribute">An instance of <see cref="T:System.ObsoleteAttribute" /> if the type has been marked as obsolete.</param>
145+
/// <returns><c>true</c> if the member has been marked as obsolete; otherwise <c>false</c>.</returns>
146+
public static bool IsObsolete(Type type, out ObsoleteAttribute attribute) {
147+
attribute = type?.GetTypeInfo().GetCustomAttributes(true).OfType<ObsoleteAttribute>().FirstOrDefault();
148+
return attribute != null;
149+
}
150+
131151
/// <summary>
132152
/// Returns whether <typeparamref name="T"/> is marked as obsolete.
133153
/// </summary>
@@ -148,6 +168,16 @@ public static bool IsObsolete<T>(out ObsoleteAttribute attribute) {
148168
return attribute != null;
149169
}
150170

171+
/// <summary>
172+
/// Returns whether the specified enum <paramref name="value"/> has an attribute of type <typeparamref name="T"/>.
173+
/// </summary>
174+
/// <typeparam name="T">The type of the attribute.</typeparam>
175+
/// <param name="value">The enum value.</param>
176+
/// <returns><c>true</c>c> if an attribute is found; otherwise <c>false</c>.</returns>
177+
public static bool HasCustomAttribute<T>(Enum value) where T : Attribute {
178+
return HasCustomAttribute<T>(value, out _);
179+
}
180+
151181
/// <summary>
152182
/// Returns whether the specified enum <paramref name="value"/> has an attribute of type <typeparamref name="T"/>.
153183
/// </summary>
@@ -209,6 +239,16 @@ public static T[] GetCustomAttributes<T>(Enum value) where T : Attribute {
209239

210240
}
211241

242+
/// <summary>
243+
/// Returns whether the specified <paramref name="member"/> has an attribute of type <typeparamref name="T"/>.
244+
/// </summary>
245+
/// <typeparam name="T">The type of the attribute.</typeparam>
246+
/// <param name="member">The member.</param>
247+
/// <returns><c>true</c>c> if an attribute is found; otherwise <c>false</c>.</returns>
248+
public static bool HasCustomAttribute<T>(MemberInfo member) where T : Attribute {
249+
return HasCustomAttribute<T>(member, out _);
250+
}
251+
212252
/// <summary>
213253
/// Returns whether the specified <paramref name="member"/> has an attribute of type <typeparamref name="T"/>.
214254
/// </summary>
@@ -233,11 +273,45 @@ public static bool HasCustomAttributes<T>(MemberInfo member, out T[] result) whe
233273
return result.Length > 0;
234274
}
235275

276+
/// <summary>
277+
/// Returns the first attribute of type <typeparamref name="T"/>, or <c>null</c> if no matching attributes are found.
278+
/// </summary>
279+
/// <typeparam name="T">The type of the attribute to return.</typeparam>
280+
/// <param name="member">The member holding the attribute.</param>
281+
/// <returns>An instance of <typeparamref name="T"/>, or <c>null</c> if no matching attributes are found.</returns>
282+
public static T GetCustomAttribute<T>(MemberInfo member) where T : Attribute {
283+
return member?
284+
.GetCustomAttributes<T>(false)
285+
.FirstOrDefault();
286+
}
287+
288+
/// <summary>
289+
/// Returns an array of attributes of type <typeparamref name="T"/>.
290+
/// </summary>
291+
/// <typeparam name="T">The type of the attributes to return.</typeparam>
292+
/// <param name="member">The member holding the attributes.</param>
293+
/// <returns>An array of <typeparamref name="T"/>.</returns>
294+
public static T[] GetCustomAttributes<T>(MemberInfo member) where T : Attribute {
295+
return member?
296+
.GetCustomAttributes<T>(false)
297+
.ToArray() ?? new T[0];
298+
}
299+
300+
/// <summary>
301+
/// Returns whether the specified <paramref name="type"/> has an attribute of type <typeparamref name="T"/>.
302+
/// </summary>
303+
/// <typeparam name="T">The type of the attribute.</typeparam>
304+
/// <param name="type">The type to check.</param>
305+
/// <returns><c>true</c>c> if an attribute is found; otherwise <c>false</c>.</returns>
306+
public static bool HasCustomAttribute<T>(Type type) where T : Attribute {
307+
return HasCustomAttribute<T>(type, out _);
308+
}
309+
236310
/// <summary>
237311
/// Returns whether the specified <paramref name="type"/> has an attribute of type <typeparamref name="T"/>.
238312
/// </summary>
239313
/// <typeparam name="T">The type of the attribute.</typeparam>
240-
/// <param name="type">The type.</param>
314+
/// <param name="type">The type to check.</param>
241315
/// <param name="result">The first attribute of <typeparamref name="T"/>, or <c>null</c> if no matches.</param>
242316
/// <returns><c>true</c>c> if an attribute is found; otherwise <c>false</c>.</returns>
243317
public static bool HasCustomAttribute<T>(Type type, out T result) where T : Attribute {
@@ -249,14 +323,40 @@ public static bool HasCustomAttribute<T>(Type type, out T result) where T : Attr
249323
/// Returns whether the specified <paramref name="type"/> has one or more attributes of type <typeparamref name="T"/>.
250324
/// </summary>
251325
/// <typeparam name="T">The type of the attributes.</typeparam>
252-
/// <param name="type">The type.</param>
326+
/// <param name="type">The type to check.</param>
253327
/// <param name="result">When this method returns, an array containing the matched attributes.</param>
254328
/// <returns><c>true</c>c> if one or more attributes are found; otherwise <c>false</c>.</returns>
255329
public static bool HasCustomAttributes<T>(Type type, out T[] result) where T : Attribute {
256330
result = type?.GetTypeInfo().GetCustomAttributes<T>(false).ToArray() ?? new T[0];
257331
return result.Length > 0;
258332
}
259333

334+
/// <summary>
335+
/// Returns the first attribute of type <typeparamref name="T"/>, or <c>null</c> if no matching attributes are found.
336+
/// </summary>
337+
/// <typeparam name="T">The type of the attribute to return.</typeparam>
338+
/// <param name="type">The type holding the attribute.</param>
339+
/// <returns>An instance of <typeparamref name="T"/>, or <c>null</c> if no matching attributes are found.</returns>
340+
public static T GetCustomAttribute<T>(Type type) where T : Attribute {
341+
return type?
342+
.GetTypeInfo()
343+
.GetCustomAttributes<T>(false)
344+
.FirstOrDefault();
345+
}
346+
347+
/// <summary>
348+
/// Returns an array of attributes of type <typeparamref name="T"/>.
349+
/// </summary>
350+
/// <typeparam name="T">The type of the attributes to return.</typeparam>
351+
/// <param name="type">The type holding the attributes.</param>
352+
/// <returns>An array of <typeparamref name="T"/>.</returns>
353+
public static T[] GetCustomAttributes<T>(Type type) where T : Attribute {
354+
return type?
355+
.GetTypeInfo()
356+
.GetCustomAttributes<T>(false)
357+
.ToArray() ?? new T[0];
358+
}
359+
260360
}
261361

262362
}

0 commit comments

Comments
 (0)