Repository navigation
Improve ParameterView cast exception to name the parameter and types - #68790
SergioAlmeida29 wants to merge 5 commits into
Conversation
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>
|
Thanks for your PR, @SergioAlmeida29. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
@dotnet-policy-service agree |
| result = (TValue)entry.Value; | ||
| return true; | ||
| } | ||
| catch (InvalidCastException ex) |
There was a problem hiding this comment.
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.
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
|
Thanks for the contribution. The default parameter-setting path already reports the parameter name, and |
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 fromParameterView.TryGetValue<TValue>as the runtime's bareUnable 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 anInvalidCastExceptionnaming the parameter plus source/target types, preserving the original exception asInnerException. Null supplied for a non-nullable value type receives the same contextualInvalidCastException, preserving the originalNullReferenceExceptionasInnerException. Null values remain valid for reference and nullable value types. No public API surface changes.Fixes #67285