Beispiel #1
0
        /// <summary>Converts the given object to the type of this converter, using the specified context and culture information.</summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. </param>
        /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo" /> to use as the current culture. </param>
        /// <param name="value">The <see cref="T:System.Object" /> to convert. </param>
        /// <returns>An <see cref="T:System.Object" /> that represents the converted value.</returns>
        // Token: 0x060007E2 RID: 2018 RVA: 0x00012420 File Offset: 0x00010620
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string text = value as string;

            if (text == null)
            {
                return(base.ConvertFrom(context, culture, value));
            }
            if (culture == null)
            {
                culture = CultureInfo.CurrentCulture;
            }
            string[]        array           = text.Split(culture.TextInfo.ListSeparator.ToCharArray());
            SingleConverter singleConverter = new SingleConverter();

            float[] array2 = new float[array.Length];
            for (int i = 0; i < array2.Length; i++)
            {
                array2[i] = (float)singleConverter.ConvertFromString(context, culture, array[i]);
            }
            if (array.Length != 2)
            {
                throw new ArgumentException("Failed to parse Text(" + text + ") expected text in the format \"Width,Height.\"");
            }
            return(new SizeF(array2[0], array2[1]));
        }
Beispiel #2
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string s = value as string;

            if (s == null)
            {
                return(base.ConvertFrom(context, culture, value));
            }

            if (culture == null)
            {
                culture = CultureInfo.CurrentCulture;
            }
            string[] subs = s.Split(culture.TextInfo.ListSeparator.ToCharArray());

            SingleConverter converter = new SingleConverter();

            float[] numSubs = new float[subs.Length];
            for (int i = 0; i < numSubs.Length; i++)
            {
                numSubs[i] = (float)converter.ConvertFromString(context, culture, subs[i]);
            }

            if (subs.Length != 2)
            {
                throw new ArgumentException("Failed to parse Text(" + s + ") expected text in the format \"Width,Height.\"");
            }

            return(new SizeF(numSubs[0], numSubs[1]));
        }
    internal Unit(string value, CultureInfo culture, UnitType defaultType)
    {
        if (string.IsNullOrEmpty(value))
        {
            this.@value = 0;
            this.type   = (UnitType)0;
            return;
        }
        if (culture == null)
        {
            culture = CultureInfo.CurrentCulture;
        }
        string lower  = value.Trim().ToLower(CultureInfo.InvariantCulture);
        int    length = lower.Length;
        int    num    = -1;

        for (int i = 0; i < length; i++)
        {
            char chr = lower[i];
            if ((chr < '0' || chr > '9') && chr != '-' && chr != '.' && chr != ',')
            {
                break;
            }
            num = i;
        }
        if (num == -1)
        {
            object[] objArray = new object[] { value };
            throw new FormatException(SR.GetString("UnitParseNoDigits", objArray));
        }
        if (num >= length - 1)
        {
            this.type = defaultType;
        }
        else
        {
            this.type = Unit.GetTypeFromString(lower.Substring(num + 1).Trim());
        }
        string str = lower.Substring(0, num + 1);

        try
        {
            TypeConverter singleConverter = new SingleConverter();
            this.@value = (double)((float)singleConverter.ConvertFromString(null, culture, str));
            if (this.type == UnitType.Pixel)
            {
                this.@value = (double)((int)this.@value);
            }
        }
        catch
        {
            object[] objArray1 = new object[] { value, str, this.type.ToString("G") };
            throw new FormatException(SR.GetString("UnitParseNumericPart", objArray1));
        }
        if (this.@value < -32768 || this.@value > 32767)
        {
            throw new ArgumentOutOfRangeException("value");
        }
    }
Beispiel #4
0
        public void RoundTripMinValueTest()
        {
            var converter = new SingleConverter();
            var s         = converter.ConvertToString(float.MinValue, null, new MemberMapData(null));
            var f         = converter.ConvertFromString(s, null, new MemberMapData(null));

            Assert.Equal(float.MinValue, f);
        }
Beispiel #5
0
 internal Unit(string value, CultureInfo culture, UnitType defaultType)
 {
     if (string.IsNullOrEmpty(value))
     {
         this.value = 0.0;
         this.type  = (UnitType)0;
     }
     else
     {
         if (culture == null)
         {
             culture = CultureInfo.CurrentCulture;
         }
         string str    = value.Trim().ToLower(CultureInfo.InvariantCulture);
         int    length = str.Length;
         int    num2   = -1;
         for (int i = 0; i < length; i++)
         {
             char ch = str[i];
             if (((ch < '0') || (ch > '9')) && (((ch != '-') && (ch != '.')) && (ch != ',')))
             {
                 break;
             }
             num2 = i;
         }
         if (num2 == -1)
         {
             throw new FormatException(System.Web.SR.GetString("UnitParseNoDigits", new object[] { value }));
         }
         if (num2 < (length - 1))
         {
             this.type = GetTypeFromString(str.Substring(num2 + 1).Trim());
         }
         else
         {
             this.type = defaultType;
         }
         string text = str.Substring(0, num2 + 1);
         try
         {
             TypeConverter converter = new SingleConverter();
             this.value = (float)converter.ConvertFromString(null, culture, text);
             if (this.type == UnitType.Pixel)
             {
                 this.value = (int)this.value;
             }
         }
         catch
         {
             throw new FormatException(System.Web.SR.GetString("UnitParseNumericPart", new object[] { value, text, this.type.ToString("G") }));
         }
         if ((this.value < -32768.0) || (this.value > 32767.0))
         {
             throw new ArgumentOutOfRangeException("value");
         }
     }
 }
Beispiel #6
0
        private void Init(string value, CultureInfo culture, RVUnitType defaultType)
        {
            if (culture == null)
            {
                culture = CultureInfo.CurrentCulture;
            }
            string text   = value.Trim().ToLower(culture);
            int    length = text.Length;
            int    num    = -1;

            for (int i = 0; i < length; i++)
            {
                char c = text[i];
                if ((c < '0' || c > '9') && c != '-' && c != '.' && c != ',')
                {
                    break;
                }
                num = i;
            }
            if (num == -1)
            {
                throw new FormatException();
            }
            if (num < length - 1)
            {
                m_type = GetTypeFromString(text.Substring(num + 1).Trim());
            }
            else
            {
                if (defaultType == (RVUnitType)0)
                {
                    throw new FormatException();
                }
                m_type = defaultType;
            }
            string text2 = text.Substring(0, num + 1);

            try
            {
                TypeConverter typeConverter = new SingleConverter();
                m_value = (float)typeConverter.ConvertFromString(null, culture, text2);
            }
            catch (Exception ex)
            {
                Exception ex2 = FindStoppingException(ex);
                if (ex2 == ex)
                {
                    throw;
                }
                if (ex2 != null)
                {
                    throw ex2;
                }
                throw new FormatException();
            }
        }
Beispiel #7
0
        /// <summary>
        /// Converts the specified value to a <see cref="SizeF"/>
        /// </summary>
        /// <param name="context">Conversion context</param>
        /// <param name="culture">Culture to perform the conversion</param>
        /// <param name="value">Value to convert</param>
        /// <returns>A new instance of a <see cref="SizeF"/> converted from the specified <paramref name="value"/></returns>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            var text = value as string;

            if (text != null)
            {
                var parts = text.Split(culture.TextInfo.ListSeparator.ToCharArray());
                if (parts.Length != 2)
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Cannot parse value '{0}' as size. Should be in the form of 'width,height'", text));
                }

                var converter = new SingleConverter();
                return(new SizeF(
                           (float)converter.ConvertFromString(context, culture, parts [0]),
                           (float)converter.ConvertFromString(context, culture, parts [1])
                           ));
            }
            return(base.ConvertFrom(context, culture, value));
        }
        private void Init(string value, CultureInfo culture, UnitType defaultType)
        {
            if (culture == null)
            {
                culture = CultureInfo.CurrentCulture;
            }

            string valueString = value.Trim().ToLower();
            int    valueLength = valueString.Length;
            int    num2        = -1;

            for (int i = 0; i < valueLength; i++)
            {
                char currentChar = valueString[i];
                if (((currentChar < '0') || (currentChar > '9')) && (((currentChar != '-') && (currentChar != '.')) && (currentChar != ',')))
                {
                    break;
                }

                num2 = i;
            }
            if (num2 == -1)
            {
                throw new FormatException(string.Format("{0} cannot be parsed as a unit because it does not contain numeric values. Examples of valid unit strings are \"1pt\" and \".5in\".", value));
            }

            if (num2 < (valueLength - 1))
            {
                _type = Unit.GetTypeFromString(valueString.Substring(num2 + 1).Trim());
            }
            else
            {
                if (defaultType == ((UnitType)0))
                {
                    throw new FormatException(string.Format("{0} does not contain a unit specification. Examples of valid unit strings are \"1pt\" and \".5in\".", value));
                }

                _type = defaultType;
            }

            string valueSubstring = valueString.Substring(0, num2 + 1);

            try
            {
                TypeConverter converter = new SingleConverter();
                _value = (float)converter.ConvertFromString(null, CultureInfo.InvariantCulture, valueSubstring);
            }
            catch
            {
                throw new FormatException(string.Format("The numeric portion of {0} cannot be parsed as a unit of type {1}.", value, _type.ToString("G")));
            }
        }
Beispiel #9
0
        /// <summary>
        /// Tries to parse a 32-bit single precision float from a string. This method should handle hexadecimal values
        /// as well as normal values.
        /// </summary>
        /// <param name="value">The string value to parse.</param>
        /// <param name="result">The parsed float value, if the string was valid. If invalid, this
        /// will be the default float value value.</param>
        /// <returns>True if the conversion was successful; otherwise returns false.</returns>
        public static bool TryParseEx(string value, out float result)
        {
            bool canConvert = true;

            try
            {
                var converter = new SingleConverter();
                result = (float)converter.ConvertFromString(value);
            }
            catch (Exception)
            {
                result     = default(float);
                canConvert = false;
            }

            return(canConvert);
        }
Beispiel #10
0
        internal Unit(string value, CultureInfo culture, UnitType defaultType)
        {
            if (string.IsNullOrEmpty(value))
            {
                _value = 0.0f;
                _type  = (UnitType)0;
            }
            else
            {
                if (culture == null)
                {
                    culture = CultureInfo.CurrentCulture;
                }

                string str    = value.Trim().ToLower(CultureInfo.InvariantCulture);
                int    length = str.Length;
                int    num2   = -1;
                for (int i = 0; i < length; i++)
                {
                    char ch = str[i];
                    if (((ch < '0') || (ch > '9')) && (((ch != '-') && (ch != '.')) && (ch != ',')))
                    {
                        break;
                    }
                    num2 = i;
                }
                if (num2 == -1)
                {
                    throw new FormatException(string.Format("'{0}' cannot be parsed as a unit as there are no numeric values in it. Examples of valid unit strings are '30px' and '50%'.", value));
                }

                if (num2 < (length - 1))
                {
                    _type = GetTypeFromString(str.Substring(num2 + 1).Trim());
                }
                else
                {
                    _type = defaultType;
                }

                string text = str.Substring(0, num2 + 1);
                try
                {
                    TypeConverter converter = new SingleConverter();
                    _value = (float)converter.ConvertFromString(null, culture, text);
                    if (_type == UnitType.Pixel)
                    {
                        _value = (int)_value;
                    }
                }
                catch
                {
                    throw new FormatException(string.Format("The numeric part ('{1}') of '{0}' cannot be parsed as a numeric part of a {2} unit.", value, text, _type.ToString("G")));
                }

                if ((_value < -32768.0) || (_value > 32767.0))
                {
                    throw new ArgumentOutOfRangeException("value");
                }
            }
        }
Beispiel #11
0
        internal Unit(string value, CultureInfo culture, UnitType defaultType)
        {
            if ((value == null) || (value.Length == 0))
            {
                this.value = 0;
                this.type  = (UnitType)0;
            }
            else
            {
                if (culture == null)
                {
                    culture = CultureInfo.CurrentCulture;
                }

                // This is invariant because it acts like an enum with a number together.
                // The enum part is invariant, but the number uses current culture.
                string trimLcase = value.Trim().ToLower(CultureInfo.InvariantCulture);
                int    len       = trimLcase.Length;

                int lastDigit = -1;
                for (int i = 0; i < len; i++)
                {
                    char ch = trimLcase[i];
                    if (((ch < '0') || (ch > '9')) && (ch != '-') && (ch != '.') && (ch != ','))
                    {
                        break;
                    }
                    lastDigit = i;
                }
                if (lastDigit == -1)
                {
                    throw new FormatException(SR.GetString(SR.UnitParseNoDigits, value));
                }
                if (lastDigit < len - 1)
                {
                    type = (UnitType)GetTypeFromString(trimLcase.Substring(lastDigit + 1).Trim());
                }
                else
                {
                    type = defaultType;
                }

                string numericPart = trimLcase.Substring(0, lastDigit + 1);
                // Cannot use Double.FromString, because we don't use it in the ToString implementation
                try {
                    if (type == UnitType.Pixel)
                    {
                        TypeConverter converter = new Int32Converter();
                        this.value = (int)converter.ConvertFromString(null, culture, numericPart);
                    }
                    else
                    {
                        TypeConverter converter = new SingleConverter();
                        this.value = (Single)converter.ConvertFromString(null, culture, numericPart);
                    }
                }
                catch {
                    throw new FormatException(SR.GetString(SR.UnitParseNumericPart, value, numericPart, type.ToString("G")));
                }
                if ((this.value < MinValue) || (this.value > MaxValue))
                {
                    throw new ArgumentOutOfRangeException("value");
                }
            }
        }