Ejemplo n.º 1
0
        /// <summary>
        ///     Convert between any two quantity units by their abbreviations, such as converting a "Length" of N "m" to "cm".
        ///     This is particularly useful for creating things like a generated unit conversion UI,
        ///     where you list some selectors:
        ///     a) Quantity: Length, Mass, Force etc.
        ///     b) From unit: Meter, Centimeter etc if Length is selected
        ///     c) To unit: Meter, Centimeter etc if Length is selected
        /// </summary>
        /// <param name="fromValue">
        ///     Input value, which together with <paramref name="fromUnitAbbrev" /> represents the quantity to
        ///     convert from.
        /// </param>
        /// <param name="quantityName">
        ///     Name of quantity, such as "Length" and "Mass". <see cref="QuantityType" /> for all
        ///     values.
        /// </param>
        /// <param name="fromUnitAbbrev">
        ///     Name of unit, such as "Meter" or "Centimeter" if "Length" was passed as
        ///     <paramref name="quantityName" />.
        /// </param>
        /// <param name="toUnitAbbrev">
        ///     Name of unit, such as "Meter" or "Centimeter" if "Length" was passed as
        ///     <paramref name="quantityName" />.
        /// </param>
        /// <param name="culture">Culture to parse abbreviations with.</param>
        /// <example>double centimeters = ConvertByName(5, "Length", "m", "cm"); // 500</example>
        /// <returns>Output value as the result of converting to <paramref name="toUnitAbbrev" />.</returns>
        /// <exception cref="QuantityNotFoundException">No quantity types match the <paramref name="quantityName"/>.</exception>
        /// <exception cref="UnitNotFoundException">No unit types match the prefix of <paramref name="quantityName"/> or no units are mapped to the abbreviation.</exception>
        /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbrevation.</exception>
        public static double ConvertByAbbreviation(FromValue fromValue, string quantityName, string fromUnitAbbrev, string toUnitAbbrev, string culture)
        {
            Type quantityType = GetQuantityType(quantityName);
            Type unitType     = GetUnitType(quantityName);

            UnitSystem unitSystem    = UnitSystem.GetCached(culture);
            object     fromUnitValue = unitSystem.Parse(fromUnitAbbrev, unitType);               // ex: ("m", LengthUnit) => LengthUnit.Meter
            object     toUnitValue   = unitSystem.Parse(toUnitAbbrev, unitType);                 // ex:("cm", LengthUnit) => LengthUnit.Centimeter

            MethodInfo fromMethod = GetStaticFromMethod(quantityType, unitType);                 // ex: UnitsNet.Length.From(double inputValue, LengthUnit inputUnit)
            object     fromResult = fromMethod.Invoke(null, new[] { fromValue, fromUnitValue }); // ex: Length quantity = UnitsNet.Length.From(5, LengthUnit.Meter)

            MethodInfo asMethod = GetAsMethod(quantityType, unitType);                           // ex: quantity.As(LengthUnit outputUnit)
            object     asResult = asMethod.Invoke(fromResult, new[] { toUnitValue });            // ex: double outputValue = quantity.As(LengthUnit.Centimeter)

            return((double)asResult);
        }