Пример #1
0
        /// <summary>
        ///     Parse a unit string.
        /// </summary>
        /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
        /// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
        /// <example>
        ///     Length.ParseUnit("m", new CultureInfo("en-US"));
        /// </example>
        /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
        /// <exception cref="UnitsNetException">Error parsing string.</exception>
        public static AreaMomentOfInertiaUnit ParseUnit(string str, IFormatProvider provider = null)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            var unitSystem = UnitSystem.GetCached(provider);
            var unit       = unitSystem.Parse <AreaMomentOfInertiaUnit>(str.Trim());

            if (unit == AreaMomentOfInertiaUnit.Undefined)
            {
                var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized AreaMomentOfInertiaUnit.");
                newEx.Data["input"]    = str;
                newEx.Data["provider"] = provider?.ToString() ?? "(null)";
                throw newEx;
            }

            return(unit);
        }
        /// <summary>
        ///     Parse a unit string.
        /// </summary>
        /// <example>
        ///     Length.ParseUnit("m", new CultureInfo("en-US"));
        /// </example>
        /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
        /// <exception cref="UnitsNetException">Error parsing string.</exception>
        public static TemperatureChangeRateUnit ParseUnit(string str, IFormatProvider formatProvider = null)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            var unitSystem = UnitSystem.GetCached(formatProvider);

            var unit = unitSystem.Parse <TemperatureChangeRateUnit>(str.Trim());

            if (unit == TemperatureChangeRateUnit.Undefined)
            {
                var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized TemperatureChangeRateUnit.");
                newEx.Data["input"]          = str;
                newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString();
                throw newEx;
            }

            return(unit);
        }
Пример #3
0
        static DensityUnit ParseUnit(string str, IFormatProvider formatProvider = null)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }

            var unitSystem = UnitSystem.GetCached(formatProvider);
            var unit       = unitSystem.Parse <DensityUnit>(str.Trim());

            if (unit == DensityUnit.Undefined)
            {
                var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized DensityUnit.");
                newEx.Data["input"]          = str;
                newEx.Data["formatprovider"] = formatProvider?.ToString() ?? "(null)";
                throw newEx;
            }

            return(unit);
        }
Пример #4
0
        /// <summary>
        ///     Parse a unit string.
        /// </summary>
        /// <param name="str">String to parse. Typically in the form: {number} {unit}</param>
        /// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param>
        /// <example>
        ///     Length.ParseUnit("m", new CultureInfo("en-US"));
        /// </example>
        /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception>
        /// <exception cref="UnitsNetException">Error parsing string.</exception>
        internal static HeatTransferCoefficientUnit ParseUnit(string str, IFormatProvider provider = null)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }

            var unitSystem = UnitSystem.GetCached(provider);
            var unit       = unitSystem.Parse <HeatTransferCoefficientUnit>(str.Trim());

            if (unit == HeatTransferCoefficientUnit.Undefined)
            {
                var newEx = new UnitsNetException("Error parsing string. The unit is not a recognized HeatTransferCoefficientUnit.");
                newEx.Data["input"]    = str;
                newEx.Data["provider"] = provider?.ToString() ?? "(null)";
                throw newEx;
            }

            return(unit);
        }