Reputation: 11594
I am using reflection and I want to do conversion checking for exception handling.
I need to check if string can convert to unknown type in my project.
I used :
TypeConverter t = TypeDescriptor.GetConverter(typeof(string));
Console.WriteLine(t.CanConvertTo(typeof(int)));
but it returns false !
or even this one returns false again :
StringConverter stringConverter = new StringConverter();
Console.WriteLine(stringConverter.CanConvertTo(typeof(int)));
my quesion is that why StringConverter returns false for converting string to int ???
EDIT :
I use this code to convert string to unknown types : ( result is string )
resultCastedToTargetPropertyType = Convert.ChangeType(result,propertyInfo.PropertyType);
Upvotes: 2
Views: 1489
Reputation: 23819
The docs for StringConverter
state:
This converter can only convert to a string. It works as a pass through for other converters that want to convert an object to a string.
Int32Converter
, on the other hand, states:
This converter can only convert a 32-bit signed integer object to and from a string. The Int32 value type represents signed integers with values ranging from negative 2,147,483,648 through positive 2,147,483,647.
As such Int32Converter
is likely what you want to use, rather than StringConverter
.
Alternatively, consider writing a Convert.TryChangeType
extension method, with a try catch
block in it - this will avoid you having to worry about the individual converters. In effect you will 'try it and see'. This is especially important when doing conversions where you aren't sure whether they will work until runtime (e.g "abc" to int).
Upvotes: 1
Reputation: 726489
Oddities of the implementation of CanConvertFrom
aside, in your context you wouldn't get much help by knowing that a certain conversion could be performed until you try it. A converter cannot give you a definitive answer based on the type alone, for the same reason that there is no single answer to the question "can a string be converted to integer": the answer is "it depends on the string" - i.e. string "123"
can be converted to int
, while strings "hello"
and "1234567891011121314151617181920"
cannot be converted to int
.
In other words, the answer could be obtained only when you know the value being converted. In your case it means putting try
/catch
block around your call of Convert.ChangeType
, and interpreting each of the four exceptions it could throw (reference).
Upvotes: 3
Reputation: 1062590
A StringConverter
only cares about string
. What you would need is System.ComponentModel.Int32Converter
. It is Int32Converter
that cares about int
. TypeDescriptor.GetConverter(typeof(int))
gives you that.
Essentially, a TypeConverter
is usually used to format a target type to display, or to parse input back to that target type. There's nothing much involved in displaying or parsing a string to/from a string, so you will be unsurprised to hear that StringConverter
does very little! Here, the interesting type is int
, and Int32Converter
knows how to format an int
for display, and parse an int
from input.
Upvotes: 1