Skip to content

Commit

Permalink
[Performance] Ensure ReflectionOperations return Attribute[] when p…
Browse files Browse the repository at this point in the history
…ossible (#4271)
  • Loading branch information
Youssef1313 authored Dec 9, 2024
1 parent 6b1017d commit fcee02a
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if !NETFRAMEWORK
using System.Diagnostics;
#endif
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

Expand Down Expand Up @@ -28,11 +31,20 @@ public class ReflectionOperations : IReflectionOperations
/// <param name="inherit"> True to inspect the ancestors of element; otherwise, false. </param>
/// <returns> The list of attributes on the member. Empty list if none found. </returns>
[return: NotNullIfNotNull(nameof(memberInfo))]
public object[]? GetCustomAttributes(MemberInfo memberInfo, bool inherit) =>
public object[]? GetCustomAttributes(MemberInfo memberInfo, bool inherit)
#if NETFRAMEWORK
ReflectionUtility.GetCustomAttributes(memberInfo, inherit).ToArray();
=> ReflectionUtility.GetCustomAttributes(memberInfo, inherit).ToArray();
#else
memberInfo.GetCustomAttributes(inherit);
{
object[] attributes = memberInfo.GetCustomAttributes(typeof(Attribute), inherit);

// Ensures that when the return of this method is used here:
// https://github.com/microsoft/testfx/blob/e101a9d48773cc935c7b536d25d378d9a3211fee/src/Adapter/MSTest.TestAdapter/Helpers/ReflectHelper.cs#L461
// then we are already Attribute[] to avoid LINQ Cast and extra array allocation.
// This assert is solely for performance. Nothing "functional" will go wrong if the assert failed.
Debug.Assert(attributes is Attribute[], $"Expected Attribute[], found '{attributes.GetType()}'.");
return attributes;
}
#endif

/// <summary>
Expand Down Expand Up @@ -60,6 +72,6 @@ public object[] GetCustomAttributes(Assembly assembly, Type type) =>
#if NETFRAMEWORK
ReflectionUtility.GetCustomAttributes(assembly, type).ToArray();
#else
assembly.GetCustomAttributes(type).ToArray<object>();
assembly.GetCustomAttributes(type, inherit: true);
#endif
}

0 comments on commit fcee02a

Please sign in to comment.