Fix InvalidCastException in Can/ConvertTo from MouseActionConverter #10381
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Mirrors #10289
Description
Sending this in now when we merged and discussed #9891 as I've discovered this bug during development of #9676 but wanted to wait until I can fix the TODOs in the unit tests as well.
Fixes
InvalidCastException
thrown fromCanConvertTo
when unboxing cast is performed on value that was notnull
but was of a different type thanMouseAction
or the underlying storage (byte
), e.g. an object or a different value type (int
).Can* methods should not (and converters do not intentionally) throw in them but this is simple a programmer's error, it is not documented behavior either, hence the fix for this.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.mouseactionconverter.canconvertto?view=windowsdesktop-9.0
Fixes the same issue in
ConvertTo
by throwing the proper (NotSupported) exception.https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.mouseactionconverter.convertto?view=windowsdesktop-9.0
Customer Impact
Less unexpected exceptions thrown.
Regression
No.
Testing
Local build, unit test.
Risk
There might be a concern that a code was passing
byte
instead ofMouseAction
which is the underlying storage type, so it survives the unboxing hard cast that was being performed but not the typeis
check. We may therefore for back-compat include check for bothMouseAction
andbyte
but other converters (besidesKey
since it shares the bug) just check for their type, so in my honest opinion it would be best to unify the behaviour and mention in release notes.Not to mention, unlike
Key
where the backing storage isint
, here itsbyte
, even lesser chance for such occurence.Microsoft Reviewers: Open in CodeFlow