Ejemplo n.º 1
0
        public override object ConvertFrom(ITypeDescriptorContext context,
                                           CultureInfo culture, object value)
        {
            if (value is string)
            {
                try
                {
                    string s = (string)value;

                    if (type == null)
                    {
                        type = context.PropertyDescriptor.PropertyType;
                    }

                    BaseChangeableNumber bcn = BaseChangeableNumber.Convert(s, type);
                    if (context.PropertyDescriptor.PropertyType == typeof(long))
                    {
                        return(bcn.LongValue);
                    }
                    if (context.PropertyDescriptor.PropertyType == typeof(ulong))
                    {
                        return((ulong)bcn.LongValue);
                    }
                    if (context.PropertyDescriptor.PropertyType == typeof(int))
                    {
                        return(bcn.IntegerValue);
                    }
                    if (context.PropertyDescriptor.PropertyType == typeof(uint))
                    {
                        return((uint)bcn.IntegerValue);
                    }
                    if (context.PropertyDescriptor.PropertyType == typeof(short))
                    {
                        return((short)bcn.IntegerValue);
                    }
                    if (context.PropertyDescriptor.PropertyType == typeof(ushort))
                    {
                        return((ushort)bcn.IntegerValue);
                    }
                    if (context.PropertyDescriptor.PropertyType == typeof(byte))
                    {
                        return((byte)bcn.IntegerValue);
                    }
                    if (context.PropertyDescriptor.PropertyType == typeof(sbyte))
                    {
                        return((sbyte)bcn.IntegerValue);
                    }
                    return(bcn);
                }
                catch
                {
                    throw new ArgumentException(
                              "Can not convert '" + (string)value + "'. This is not a valid " + BaseChangeableNumber.BaseName + " Number of Type " + type.Name + "!");
                }
            }
            return(base.ConvertFrom(context, culture, value));
        }
Ejemplo n.º 2
0
        public override object ConvertTo(ITypeDescriptorContext context,
                                         CultureInfo culture,
                                         object value,
                                         System.Type destinationType)
        {
            if (destinationType == typeof(System.String) &&
                value is BaseChangeableNumber)
            {
                BaseChangeableNumber so = (BaseChangeableNumber)value;
                type = so.Type;

                return(so.ToString());
            }
            else if (destinationType == typeof(System.String))
            {
                BaseChangeableNumber so = new BaseChangeableNumber(value);
                return(so.ToString());
            }
            return(base.ConvertTo(context, culture, value, destinationType));
        }