Repository navigation
Remove duplicated reflection helpers in ReflectHelper - #9502
Conversation
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>
There was a problem hiding this comment.
Pull request overview
This PR removes duplicated reflection metadata extraction logic in ReflectHelper by delegating to the existing “live” implementations in ReflectionHelper (extension methods on IReflectionOperations). This keeps behavior centralized in the same place that TypeEnumerator uses, while preserving the ReflectHelper surface.
Changes:
- Replaced
ReflectHelper.GetTestCategoriesbody with a thin delegate toIReflectionOperations.GetTestCategories(...)(extension method). - Replaced
ReflectHelper.GetTestPropertiesAsTraitsbody with a thin delegate toIReflectionOperations.GetTestPropertiesAsTraits(...)(extension method). - Added the required
usingto bring the extension methods into scope.
Show a summary per file
| File | Description |
|---|---|
| src/Adapter/MSTestAdapter.PlatformServices/Helpers/ReflectHelper.cs | Removes duplicated implementations by delegating to ReflectionHelper extension methods on IReflectionOperations. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Low
Amaury Levé (Evangelink)
left a comment
There was a problem hiding this comment.
Note
🤖 Automated review by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.
✅ 22/22 dimensions clean — no findings.
Behavioral equivalence — verified ✔
GetTestCategories: The old body called ReflectHelper.GetCustomAttributesCached(method) / (owningType) / (owningType.Assembly), which is a static shim that already delegated to PlatformServiceProvider.Instance.ReflectionOperations.GetCustomAttributesCached(...). The new one-liner delegates directly to PlatformServiceProvider.Instance.ReflectionOperations.GetTestCategories(categoryAttributeProvider, owningType), which is the extension method in ReflectionHelper — that method calls reflectionOperations.GetCustomAttributesCached(...) on the same instance, hitting the same ConcurrentDictionary cache. Attribute order (method → type → assembly) and the lazy-List<string> / spread-to-array return pattern are identical.
GetTestPropertiesAsTraits: The old body used GetCustomAttributesCached(testPropertyProvider) for the method and testPropertyProvider.ReflectedType is { } testClass ? GetCustomAttributesCached(testClass) : [] for the class. The new one-liner delegates to ReflectionHelper.GetTestPropertiesAsTraits, which uses the identical null-guarded pattern via reflectionOperations.GetCustomAttributesCached. The two-pass count-then-fill algorithm and the empty-array fast path are preserved verbatim.
The PR's zero-behavior-change claim is correct.
Summary
Addresses part of #9499 (Code Duplication Analysis).
ReflectHelper.GetTestCategoriesandReflectHelper.GetTestPropertiesAsTraits(static methods) duplicated the algorithm bodies of theIReflectionOperationsextension methods of the same name inReflectionHelper. The staticReflectHelper.GetCustomAttributesCachedhelper already delegates toPlatformServiceProvider.Instance.ReflectionOperations, so the two static methods are now thin delegates to the shared extension methods — removing ~70 lines of true duplication with zero behavior change.The
ReflectionHelperextension methods are the "live" implementations (used byTypeEnumerator);ReflectHelper.GetTestCategories(static) had no production callers andReflectHelper.GetTestPropertiesAsTraits(static) is only referenced from a unit test, which keeps working via the delegate.Scope notes
The dedup-analysis issue lists 21 candidates. The remaining items were intentionally left as-is:
ReflectHelpervsReflectionOperations: intentional architectural split (singleton facade vs service); merging would break theReflectHelper.Instancesurface.SynchronizationContextPreservingAsyncTaskMethodBuilder: generic/non-generic struct pair required by the compile-time async-builder pattern; cannot be unified.Initializeboilerplate by convention; extracting a base for just two would be locally inconsistent.Validation
MSTestAdapter.PlatformServicesbuilds clean (0 warnings, 0 errors).ReflectionOperationsTests(47) andTestPropertyAttributeTests(3) pass.Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com