-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SizeConverter is trying to convert even before verifying the value could be converted #92424
Comments
Tagging subscribers to this area: @dotnet/area-system-componentmodel Issue DetailsDescriptionTwo issues here: CanConvertFrom returns true when the input parameter is a string. This is not correct:
ConvertFrom is trying to convert the values even the list of values provided has not 2 elements This does not look "friendly". In case there are problems with the list separator (different culture when the value was created than when it is consumed), it will be much more useful raise the exception before trying to convert the elements with a message describing the problem: Reproduction StepsTry to convert "1024,768" when regional settings are es_ES On es_ES, the list separator is semi-colon. So, "1024,768".split(";") returns "1024,768" and then the try to convert to int crashes but the message is confuse, because 1024,768 is a valid number on Spanish. Expected behaviorTry to convert "1024,768" when regional settings are es_ES throws an exception with a message like: Actual behaviorTry to convert "1024,768" when regional settings are es_ES throws an exception because 1024,768 is not a valid integer Regression?No response Known WorkaroundsOn Windows modify the regional settings to use "," as list separator instead ";" ConfigurationNo response Other informationNo response
|
Changing the order of validation here would be breaking, but could be done to give a better error in this case. runtime/src/libraries/System.ComponentModel.TypeConverter/src/System/Drawing/SizeConverter.cs Lines 48 to 51 in 84a8f67
Regardless of what we do, we should probably fix that string, since it's explicitly using a |
So we would want something like throw new ArgumentException(SR.Format(SR.TextParseFailedFormat, text, $"Width{sep}Height")); |
I'd like to try fixing the string separator part in the Exception message, if this would help? However - it seems like the same problem exists also for Should this be filed as a new separate issue, or done within #92424? Advice appreciated, since this would be my first time contributing. |
Description
Two issues here:
CanConvertFrom returns true when the input parameter is a string.
This is not correct:
ConvertFrom is trying to convert the values even the list of values provided has not 2 elements
This does not look "friendly".
If the result of the split is not returning exactly two elements, what is the sense of trying to convert them?. Even if you are able to convert, the result will be a exception raised because the number of elements
In case there are problems with the list separator (different culture when the value was created than when it is consumed), it will be much more useful raise the exception before trying to convert the elements with a message describing the problem:
"Unable to get the expected elements (2) when splitting the string ("my string") using the list separator ("sep") from the regional settings (culture)"
Reproduction Steps
Try to convert "1024,768" when regional settings is es_ES
On es_ES, the list separator is semi-colon. So, "1024,768".split(";") returns "1024,768" and then the try to convert to int crashes but the message is confuse, because 1024,768 is a valid number on Spanish.
Expected behavior
Try to convert "1024,768" when regional settings are es_ES throws an exception with a message like:
"Unable to create a Size from '1024,768' string because it was not possible to extract the required two components using the separator ';' from culture 'es_ES'"
Actual behavior
Try to convert "1024,768" when regional settings are es_ES throws an exception because 1024,768 is not a valid integer
Regression?
No response
Known Workarounds
On Windows modify the regional settings to use "," as list separator instead ";"
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: