Skip to content

SizeConverter is trying to convert even before verifying the value could be converted #92424

@db410009

Description

@db410009

Description

Two issues here:

CanConvertFrom returns true when the input parameter is a string.

This is not correct:

  • The string should contain exactly two elements separated by the "culture" list separator
  • It should be possible than both elements are converted into int numbers

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.ComponentModelenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions