Skip to content

Instantly share code, notes, and snippets.

@grumpydev
Last active December 12, 2015 10:48
Show Gist options
  • Save grumpydev/4761422 to your computer and use it in GitHub Desktop.
Save grumpydev/4761422 to your computer and use it in GitHub Desktop.
Mono TypeConverter bug - always returns "true" for IsValid for int types
[beep@pinky monotest]$ mono test.exe 12
Converting 12
Converted reported IsValid as True
Conversion successful 12
[beep@pinky monotest]$ mono test.exe fred
Converting fred
Converted reported IsValid as True
Conversion failed System.Exception: fred is not a valid value for Int32. ---> System.FormatException: Input string was not in the correct format
at System.Int32.Parse (System.String s, NumberStyles style, IFormatProvider provider) [0x00000] in <filename unknown>:0
at System.ComponentModel.Int32Converter.ConvertFromString (System.String value, System.Globalization.NumberFormatInfo format) [0x00000] in <filename unknown>:0
at System.ComponentModel.BaseNumberConverter.ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, System.Object value) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.ComponentModel.BaseNumberConverter.ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, System.Object value) [0x00000] in <filename unknown>:0
at System.ComponentModel.TypeConverter.ConvertFromString (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, System.String text) [0x00000] in <filename unknown>:0
at System.ComponentModel.TypeConverter.ConvertFromInvariantString (ITypeDescriptorContext context, System.String text) [0x00000] in <filename unknown>:0
at System.ComponentModel.TypeConverter.ConvertFromInvariantString (System.String text) [0x00000] in <filename unknown>:0
at Test.Main (System.String[] args) [0x00000] in <filename unknown>:0
using System;
using System.ComponentModel;
public class Test
{
public static void Main(string[] args)
{
var value = args.Length > 0 ? args[0] : "4abc2";
var converter = TypeDescriptor.GetConverter(typeof(int));
Console.WriteLine("Converting {0}", value);
Console.WriteLine("Converted reported IsValid as {0}", converter.IsValid(value));
try
{
var result = (int)converter.ConvertFromInvariantString(value);
Console.WriteLine("Conversion successful {0}", result);
}
catch (Exception ex)
{
Console.WriteLine("Conversion failed {0}", ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment