8000 [OpenApi] Use GetCustomAttributes<T> by martincostello · Pull Request #62987 · dotnet/aspnetcore · GitHub
[go: up one dir, main page]

Skip to content

Conversation

martincostello
Copy link
Member

Use GetCustomAttributes

Use GetCustomAttributes<T>().

Description

Use GetCustomAttributes<T>() instead of GetCustomAttributes().OfType<T>() to avoid need to use LINQ.

Use `GetCustomAttributes<T>()` instead of `GetCustomAttributes().OfType<ValidationAttribute>()` to avoid need to use LINQ.
@Copilot Copilot AI review requested due to automatic review settings July 29, 2025 12:15
@martincostello martincostello requested review from a team and captainsafia as code owners July 29, 2025 12:15
@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Jul 29, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jul 29, 2025
Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR modernizes the use of reflection by replacing GetCustomAttributes().OfType<T>() with the more efficient GetCustomAttributes<T>() method. This change removes the need for LINQ's OfType<T>() method and provides better performance.

  • Replaces GetCustomAttributes().OfType<ValidationAttribute>() with GetCustomAttributes<ValidationAttribute>()

}

if (parameterInfo.GetCustomAttributes().OfType<ValidationAttribute>() is { } validationAttributes)
if (parameterInfo.GetCustomAttributes<ValidationAttribute>() is { } validationAttributes)
Copy link
Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good optimization! Using GetCustomAttributes() is more efficient than GetCustomAttributes().OfType() as it avoids the overhead of boxing and LINQ enumeration.

Copilot uses AI. Check for mistakes.

@martincostello martincostello added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Jul 29, 2025
@captainsafia captainsafia merged commit 52ae1ca into dotnet:main Jul 31, 2025
31 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-rc1 milestone Jul 31, 2025
@martincostello martincostello deleted the Use-GetCustomAttributesOfT branch July 31, 2025 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc community-contribution Indicates that the PR has been added by a community member feature-openapi

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0