Skip to content

Improve ParameterView cast exception to name the parameter and types - #68790

Closed
SergioAlmeida29 wants to merge 5 commits into
dotnet:mainfrom
SergioAlmeida29:fix/67285-parameterview-cast-message
Closed

SergioAlmeida29 wants to merge 5 commits into
dotnet:mainfrom
SergioAlmeida29:fix/67285-parameterview-cast-message

Conversation

@SergioAlmeida29

@SergioAlmeida29 SergioAlmeida29 commented Aug 25, 2026 •

Copy link
Copy Markdown
  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Summary of the changes (Less than 80 chars)

Name the parameter and types in ParameterView cast exceptions

Description

With splatted attributes (@attributes="...") a type mismatch surfaced from ParameterView.TryGetValue<TValue> as the runtime's bare Unable to cast object of type 'System.Int32' to type 'System.Double'., with no indication of which parameter failed. The cast is now wrapped and rethrown as an InvalidCastException naming the parameter plus source/target types, preserving the original exception as InnerException. Null supplied for a non-nullable value type receives the same contextual InvalidCastException, preserving the original NullReferenceException as InnerException. Null values remain valid for reference and nullable value types. No public API surface changes.

Fixes #67285

When a splatted attribute value's type mismatches the receiving
parameter, ParameterView.TryGetValue<TValue> threw the runtime's bare
"Unable to cast object of type ..." message without indicating which
parameter failed. Wrap the cast and rethrow an InvalidCastException
naming the parameter plus source/target types, preserving the original
exception as InnerException. Exception type and throwing semantics are
unchanged.

Fixes dotnet#67285

Signed-off-by: Sergio Almeida <sergioalmeida29.05@gmail.com>
Copilot AI lite review requested due to automatic review settings August 25, 2026 23:38
@SergioAlmeida29
SergioAlmeida29 requested a review from a team as a code owner August 25, 2026 23:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Aug 25, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @SergioAlmeida29. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@SergioAlmeida29

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@Youssef1313 Youssef1313 added the area-blazor Includes: Blazor, Razor Components label Sep 2, 2026
result = (TValue)entry.Value;
return true;
}
catch (InvalidCastException ex)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we also handle a null value targeting a nonnullable value type? FromDictionary accepts null values, but TryGetValue<int> then throws NullReferenceException at the cast, which bypasses this catch (InvalidCastException). As a result, the contextual diagnostic is not produced, and the sourceType ?? "null" branch is unreachable for this case. I verified this through the public FromDictionary → TryGetValue<int> path. Please add a regression test and ensure this mismatch receives the contextual exception too.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 1, 2026
@kotlarmilos

Copy link
Copy Markdown
Member

Thanks for the contribution. The default parameter-setting path already reports the parameter name, and TryGetValue is called by user code that knows which parameter it requested. The diagnostic improvement does not seem necessary for the framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ParameterView unchecked type casts when splatting provides unhelpful error messages

5 participants