Skip to content

Commit 054c3c9

Browse files
Return empty array in case of missing attributes
1 parent 2467b38 commit 054c3c9

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Reflection.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ public static IEnumerable<object> GetCustomAttributes(object obj, bool shouldInh
291291
{
292292
var type = obj.GetType();
293293
var attributes = type.GetTypeInfo().GetCustomAttributes(shouldInherit);
294+
295+
if (!HasAnyAttributes(attributes, TypeAttributesCache))
296+
{
297+
return attributes;
298+
}
299+
294300
if (attributes.Length == TypeAttributesCache.Count)
295301
{
296302
return TypeAttributesCache.Values;
@@ -309,6 +315,12 @@ public static IEnumerable<object> GetCustomAttributes(object obj, bool shouldInh
309315
public static IEnumerable<object> GetCustomAttributes(MethodInfo method, bool shouldInherit = ShouldInheritAttributes)
310316
{
311317
var attributes = method.GetCustomAttributes(shouldInherit);
318+
319+
if (!HasAnyAttributes(attributes, MethodAttributesCache))
320+
{
321+
return attributes;
322+
}
323+
312324
if (attributes.Length == MethodAttributesCache.Count)
313325
{
314326
return MethodAttributesCache.Values;
@@ -757,6 +769,9 @@ private static void CacheAttributes(IEnumerable<object> attributes, ConcurrentDi
757769
}
758770
}
759771

772+
private static bool HasAnyAttributes(IReadOnlyCollection<object> attributes, ConcurrentDictionary<Type, object> result)
773+
=> attributes.Count != 0 || result.Any();
774+
760775
private static string GetFriendlyTypeName(Type type, bool useFullName)
761776
{
762777
const string anonymousTypePrefix = "<>f__";

0 commit comments

Comments
 (0)