Type Are converters broken into primitive types?

I'm having a problem with the DecimalConverter and Int32Converter , which seem to return conflicting results, as evidenced by the following simple console program:

 using System; using System.ComponentModel; class App { static void Main() { var decConverter = TypeDescriptor.GetConverter(typeof(decimal)); Console.WriteLine("Converter: {0}", decConverter.GetType().FullName); Console.WriteLine("CanConvert from int to decimal: {0}", decConverter.CanConvertFrom(typeof(int))); Console.WriteLine("CanConvert to int from decimal: {0}", decConverter.CanConvertTo(typeof(int))); Console.WriteLine(); var intConverter = TypeDescriptor.GetConverter(typeof(int)); Console.WriteLine("Converter: {0}", intConverter.GetType().FullName); Console.WriteLine("CanConvert from int to decimal: {0}", intConverter.CanConvertTo(typeof(decimal))); Console.WriteLine("CanConvert to int from decimal: {0}", intConverter.CanConvertFrom(typeof(decimal))); } } 

The way out of this value is as follows:

 Converter: System.ComponentModel.DecimalConverter CanConvert from int to decimal: False CanConvert to int from decimal: True Converter: System.ComponentModel.Int32Converter CanConvert from int to decimal: False CanConvert to int from decimal: False 

If I misunderstand TypeConverters, the following value should be true:

 TypeDescriptor.GetConverter(typeof(TypeA)).CanConvertFrom(typeof(TypeB)) 

should give the same result as

 TypeDescriptor.GetConverter(typeof(TypeB)).CanConvertTo(typeof(TypeA)) 

At least in the case of System.Int32 and System.Decimal they do not.

My question is: does anyone know if this is by design? Or are TypeConverters for native types in C # really broken?

+8
c # typeconverter
source share
2 answers

According to MSDN documentation for Int32Converter ...

This converter can only convert a 32-bit signed integer with and from a string.

I agree with @svick in the comments, however, I don't understand why you need to deserialize a JSON string through Int32 to a decimal number.

+2
source share

You do not need to deal with type converters in such cases. If you want to deserialize your model class, do something like:

 serializer.Deserialize<Model>(json) 

And he will take care of all the conversions for you.

If you really need to do the conversion manually, use Convert.ToDecimal(integer) (or Convert.ChangeType(integer, typeof(decimal)) ) and it will work correctly.

+1
source share

All Articles