using System.Globalization; namespace UnitsNet { public static partial class Quantity { private static QuantityInfoLookup Quantities => UnitsNetSetup.Default.Quantities; private static QuantityParser QuantityParser => UnitsNetSetup.Default.QuantityParser; private static UnitParser UnitParser => UnitsNetSetup.Default.UnitParser; /// /// All quantity names, such as "Length" and "Mass", that are present in the configuration. /// public static IReadOnlyCollection Names => Quantities.Names; /// /// All quantity information objects, such as and , that are present in the configuration. /// public static IReadOnlyList Infos => Quantities.Infos; /// /// All QuantityInfo instances mapped by quantity name that are present in the configuration. /// public static IReadOnlyDictionary ByName => Quantities.ByName; /// /// Get for a given unit enum value. /// public static UnitInfo GetUnitInfo(UnitKey unitEnum) => Quantities.GetUnitInfo(unitEnum); /// /// Try to get for a given unit enum value. /// public static bool TryGetUnitInfo(UnitKey unitEnum, [NotNullWhen(true)] out UnitInfo? unitInfo) { return Quantities.TryGetUnitInfo(unitEnum, out unitInfo); } /// /// Dynamically constructs a quantity from a numeric value and a unit enum value. /// /// Numeric value. /// Unit enum value. /// An object. /// Unit value is not a known unit enum type. public static IQuantity From(double value, UnitKey unit) { return Quantities.From(value, unit); } /// /// Dynamically construct a quantity from a value, the quantity name and the unit name. /// /// Numeric value. /// The invariant quantity name, such as "Length". Does not support localization. /// The invariant unit enum name, such as "Meter". Does not support localization. /// An object. /// /// Thrown when no quantity information is found for the specified quantity name. /// /// /// Thrown when no unit is found for the specified quantity name and unit name. /// public static IQuantity From(double value, string quantityName, string unitName) { return Quantities.GetUnitByName(quantityName, unitName).From(value); } /// /// Dynamically construct a quantity from a numeric value and a unit abbreviation using . /// /// /// This method is currently not optimized for performance and will enumerate all units and their unit abbreviations each time.
/// Unit abbreviation matching is case-insensitive.
///
/// This will fail if more than one unit across all quantities share the same unit abbreviation.
/// Prefer or instead. ///
/// Numeric value. /// Unit abbreviation, such as "kg" for . /// An object. /// Unit abbreviation is not known. /// Multiple units found matching the given unit abbreviation. public static IQuantity FromUnitAbbreviation(double value, string unitAbbreviation) => FromUnitAbbreviation(null, value, unitAbbreviation); /// /// Dynamically construct a quantity from a numeric value and a unit abbreviation. /// /// /// This method is currently not optimized for performance and will enumerate all units and their unit abbreviations each time.
/// Unit abbreviation matching is case-insensitive.
///
/// This will fail if more than one unit across all quantities share the same unit abbreviation.
/// Prefer or instead. ///
/// The format provider to use for lookup. Defaults to if null. /// Numeric value. /// Unit abbreviation, such as "kg" for . /// An object. /// Unit abbreviation is not known. /// Multiple units found matching the given unit abbreviation. public static IQuantity FromUnitAbbreviation(IFormatProvider? formatProvider, double value, string unitAbbreviation) { return UnitParser.FromUnitAbbreviation(value, unitAbbreviation, formatProvider); } /// /// Try to dynamically construct a quantity from a value, the quantity name and the unit name. /// /// Numeric value. /// The invariant unit enum name, such as "Meter". Does not support localization. /// The invariant quantity name, such as "Length". Does not support localization. /// The constructed quantity, if successful, otherwise null. /// True if successful with assigned the value, otherwise false. public static bool TryFrom(double value, string quantityName, string unitName, [NotNullWhen(true)] out IQuantity? quantity) { if (!Quantities.TryGetUnitByName(quantityName, unitName, out UnitInfo? unitInfo)) { quantity = null; return false; } quantity = unitInfo.From(value); return true; } /// /// Attempts to create a quantity from the specified value and unit. /// /// The value of the quantity. /// The unit of the quantity, represented as an . /// /// When this method returns, contains the created quantity if the conversion succeeded, /// or null if the conversion failed. This parameter is passed uninitialized. /// /// /// true if the quantity was successfully created; otherwise, false. /// public static bool TryFrom(double value, Enum? unit, [NotNullWhen(true)] out IQuantity? quantity) { return Quantities.TryFrom(value, unit, out quantity); } /// /// Dynamically construct a quantity from a numeric value and a unit abbreviation using . /// /// /// This method is currently not optimized for performance and will enumerate all units and their unit abbreviations each time.
/// Unit abbreviation matching is case-insensitive.
///
/// This will fail if more than one unit across all quantities share the same unit abbreviation.
/// Prefer or instead. ///
/// Numeric value. /// Unit abbreviation, such as "kg" for . /// The quantity if successful, otherwise null. /// True if successful. /// Unit value is not a known unit enum type. public static bool TryFromUnitAbbreviation(double value, string unitAbbreviation, [NotNullWhen(true)] out IQuantity? quantity) => TryFromUnitAbbreviation(null, value, unitAbbreviation, out quantity); /// /// Dynamically construct a quantity from a numeric value and a unit abbreviation. /// /// /// This method is currently not optimized for performance and will enumerate all units and their unit abbreviations each time.
/// Unit abbreviation matching is case-insensitive.
///
/// This will fail if more than one unit across all quantities share the same unit abbreviation.
/// Prefer or instead. ///
/// The format provider to use for lookup. Defaults to if null. /// Numeric value. /// Unit abbreviation, such as "kg" for . /// The quantity if successful, otherwise null. /// True if successful. /// Unit value is not a known unit enum type. public static bool TryFromUnitAbbreviation(IFormatProvider? formatProvider, double value, string unitAbbreviation, [NotNullWhen(true)] out IQuantity? quantity) { if (UnitParser.TryGetUnitFromAbbreviation(unitAbbreviation, formatProvider, out UnitInfo? unitInfo)) { quantity = unitInfo.From(value); return true; } quantity = null; return false; } /// public static IQuantity Parse(Type quantityType, string quantityString) => Parse(null, quantityType, quantityString); /// /// Dynamically parse a quantity string representation. /// /// The format provider to use for lookup. Defaults to if null. /// Type of quantity, such as . /// Quantity string representation, such as "1.5 kg". Must be compatible with given quantity type. /// The parsed quantity. /// /// Thrown when the is not of type . /// /// /// Thrown when the specified quantity type is not registered in the current configuration. /// /// Thrown when the is not in the expected format. public static IQuantity Parse(IFormatProvider? formatProvider, Type quantityType, string quantityString) { QuantityInfo quantityInfo = Quantities.GetQuantityInfo(quantityType); return QuantityParser.Parse(quantityString, formatProvider, quantityInfo); } /// public static bool TryParse(Type quantityType, string quantityString, [NotNullWhen(true)] out IQuantity? quantity) { return TryParse(null, quantityType, quantityString, out quantity); } /// public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, string quantityString, [NotNullWhen(true)] out IQuantity? quantity) { if (Quantities.TryGetQuantityInfo(quantityType, out QuantityInfo? quantityInfo)) { return QuantityParser.TryParse(quantityString, formatProvider, quantityInfo, out quantity); } quantity = null; return false; } /// /// Get a list of quantities that has the given base dimensions. /// /// The base dimensions to match. public static IEnumerable GetQuantitiesWithBaseDimensions(BaseDimensions baseDimensions) { return Infos.GetQuantitiesWithBaseDimensions(baseDimensions); } } }