2 // System.ComponentModel.BaseNumberConverter.cs
5 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 // (C) 2002/2003 Ximian, Inc (http://www.ximian.com)
8 // (C) 2003 Andreas Nahr
12 using System.Globalization;
14 namespace System.ComponentModel
16 public abstract class BaseNumberConverter : TypeConverter
19 internal Type InnerType;
21 protected BaseNumberConverter()
25 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
27 if (sourceType == typeof (string))
29 return base.CanConvertFrom (context, sourceType);
32 public override bool CanConvertTo(ITypeDescriptorContext context, Type t)
34 if (t == typeof (string))
37 return base.CanConvertTo (context, t);
40 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
42 if (value.GetType() == typeof (string)) {
44 return Convert.ChangeType (value, InnerType, culture.NumberFormat);
45 } catch (Exception e) {
46 // LAMESPEC MS just seems to pass the internal Exception on to the user
47 // so it throws a pure Exception here. We should probably throw a
48 // ArgumentException or something like that
53 return base.ConvertFrom (context, culture, value);
56 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
57 object value, Type destinationType)
60 throw new ArgumentNullException ("value");
62 if (destinationType == typeof (string) && value.GetType() == InnerType)
63 return Convert.ChangeType (value, typeof (string), culture.NumberFormat);
65 return base.ConvertTo (context, culture, value, destinationType);