From adaef37f7685a634c671c358143f99077272e9f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Mon, 29 Jun 2026 14:41:25 +0200 Subject: [PATCH] Remove duplicated reflection helpers in ReflectHelper ReflectHelper.GetTestCategories and ReflectHelper.GetTestPropertiesAsTraits duplicated the algorithm bodies of the IReflectionOperations extension methods in ReflectionHelper. Since the static GetCustomAttributesCached helper already delegates to PlatformServiceProvider.Instance.ReflectionOperations, the static methods are now thin delegates to the shared extension methods, removing ~70 lines of duplication (issue #9499). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Helpers/ReflectHelper.cs | 84 +------------------ 1 file changed, 3 insertions(+), 81 deletions(-) diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Helpers/ReflectHelper.cs b/src/Adapter/MSTestAdapter.PlatformServices/Helpers/ReflectHelper.cs index 81234c2c29..cd6c40f80c 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Helpers/ReflectHelper.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Helpers/ReflectHelper.cs @@ -6,6 +6,7 @@ #endif using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices; +using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Helpers; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -145,41 +146,7 @@ internal static bool MatchReturnType(MethodInfo method, Type returnType) /// The reflected type that owns . /// Categories defined. internal static string[] GetTestCategories(MemberInfo categoryAttributeProvider, Type owningType) - { - Attribute[] methodAttributes = GetCustomAttributesCached(categoryAttributeProvider); - Attribute[] typeAttributes = GetCustomAttributesCached(owningType); - Attribute[] assemblyAttributes = GetCustomAttributesCached(owningType.Assembly); - - // Avoid LINQ iterator allocations by iterating the cached attribute arrays directly. - // This follows the same allocation-free pattern used by GetTestPropertiesAsTraits. - List? categories = null; - - foreach (Attribute attribute in methodAttributes) - { - if (attribute is TestCategoryBaseAttribute categoryAttr) - { - (categories ??= []).AddRange(categoryAttr.TestCategories); - } - } - - foreach (Attribute attribute in typeAttributes) - { - if (attribute is TestCategoryBaseAttribute categoryAttr) - { - (categories ??= []).AddRange(categoryAttr.TestCategories); - } - } - - foreach (Attribute attribute in assemblyAttributes) - { - if (attribute is TestCategoryBaseAttribute categoryAttr) - { - (categories ??= []).AddRange(categoryAttr.TestCategories); - } - } - - return categories is null ? [] : [.. categories]; - } + => PlatformServiceProvider.Instance.ReflectionOperations.GetTestCategories(categoryAttributeProvider, owningType); /// /// Gets the parallelization level set on an assembly. @@ -232,52 +199,7 @@ internal static bool IsDoNotParallelizeSet(Assembly assembly) /// The member to inspect. /// List of traits. internal static Trait[] GetTestPropertiesAsTraits(MethodInfo testPropertyProvider) - { - Attribute[] attributesFromMethod = GetCustomAttributesCached(testPropertyProvider); - Attribute[] attributesFromClass = testPropertyProvider.ReflectedType is { } testClass ? GetCustomAttributesCached(testClass) : []; - int countTestPropertyAttribute = 0; - foreach (Attribute attribute in attributesFromMethod) - { - if (attribute is TestPropertyAttribute) - { - countTestPropertyAttribute++; - } - } - - foreach (Attribute attribute in attributesFromClass) - { - if (attribute is TestPropertyAttribute) - { - countTestPropertyAttribute++; - } - } - - if (countTestPropertyAttribute == 0) - { - // This is the common case that we optimize for. This method used to be an iterator (uses yield return) which is allocating unnecessarily in common cases. - return []; - } - - var traits = new Trait[countTestPropertyAttribute]; - int index = 0; - foreach (Attribute attribute in attributesFromMethod) - { - if (attribute is TestPropertyAttribute testProperty) - { - traits[index++] = new Trait(testProperty.Name, testProperty.Value); - } - } - - foreach (Attribute attribute in attributesFromClass) - { - if (attribute is TestPropertyAttribute testProperty) - { - traits[index++] = new Trait(testProperty.Name, testProperty.Value); - } - } - - return traits; - } + => PlatformServiceProvider.Instance.ReflectionOperations.GetTestPropertiesAsTraits(testPropertyProvider); /// /// Get attribute defined on a method which is of given type of subtype of given type.