diff --git a/UnitsNet.Tests/CustomQuantities/HowMuch.cs b/UnitsNet.Tests/CustomQuantities/HowMuch.cs index f9b39f918b..95456a9877 100644 --- a/UnitsNet.Tests/CustomQuantities/HowMuch.cs +++ b/UnitsNet.Tests/CustomQuantities/HowMuch.cs @@ -32,9 +32,9 @@ public HowMuch(double value, HowMuchUnit unit) typeof(HowMuchUnit), new UnitInfo[] { - new UnitInfo(HowMuchUnit.Some, "Some", BaseUnits.Undefined), - new UnitInfo(HowMuchUnit.ATon, "Tons", BaseUnits.Undefined), - new UnitInfo(HowMuchUnit.AShitTon, "ShitTons", BaseUnits.Undefined), + new UnitInfo(HowMuchUnit.Some, "Some", BaseUnits.Undefined, nameof(HowMuch)), + new UnitInfo(HowMuchUnit.ATon, "Tons", BaseUnits.Undefined, nameof(HowMuch)), + new UnitInfo(HowMuchUnit.AShitTon, "ShitTons", BaseUnits.Undefined, nameof(HowMuch)), }, HowMuchUnit.Some, Zero, diff --git a/UnitsNet.Tests/QuantityInfoTest.cs b/UnitsNet.Tests/QuantityInfoTest.cs index abbcb931de..420f726fcf 100644 --- a/UnitsNet.Tests/QuantityInfoTest.cs +++ b/UnitsNet.Tests/QuantityInfoTest.cs @@ -19,8 +19,8 @@ public void Constructor_AssignsProperties() { var expectedZero = Length.FromCentimeters(10); UnitInfo[] expectedUnitInfos = { - new(LengthUnit.Centimeter, "Centimeters", new BaseUnits(LengthUnit.Centimeter)), - new(LengthUnit.Kilometer, "Kilometers", new BaseUnits(LengthUnit.Kilometer)) + new(LengthUnit.Centimeter, "Centimeters", new BaseUnits(LengthUnit.Centimeter), nameof(Length)), + new(LengthUnit.Kilometer, "Kilometers", new BaseUnits(LengthUnit.Kilometer), nameof(Length)) }; var expectedBaseUnit = LengthUnit.Centimeter; var expectedBaseDimensions = Length.BaseDimensions; @@ -39,9 +39,9 @@ public void Constructor_AssignsPropertiesForCustomQuantity() { var expectedZero = new HowMuch(10, HowMuchUnit.Some); UnitInfo[] expectedUnitInfos = { - new(HowMuchUnit.Some, "Some", BaseUnits.Undefined), - new(HowMuchUnit.ATon, "Tons", BaseUnits.Undefined), - new(HowMuchUnit.AShitTon, "ShitTons", BaseUnits.Undefined) + new(HowMuchUnit.Some, "Some", BaseUnits.Undefined, nameof(HowMuch)), + new(HowMuchUnit.ATon, "Tons", BaseUnits.Undefined, nameof(HowMuch)), + new(HowMuchUnit.AShitTon, "ShitTons", BaseUnits.Undefined, nameof(HowMuch)) }; var expectedBaseUnit = HowMuchUnit.Some; var expectedBaseDimensions = BaseDimensions.Dimensionless; @@ -60,8 +60,8 @@ public void GenericsConstructor_AssignsProperties() { var expectedZero = Length.FromCentimeters(10); UnitInfo[] expectedUnitInfos = { - new(LengthUnit.Centimeter, "Centimeters", new BaseUnits(LengthUnit.Centimeter)), - new(LengthUnit.Kilometer,"Kilometers", new BaseUnits(LengthUnit.Kilometer)) + new(LengthUnit.Centimeter, "Centimeters", new BaseUnits(LengthUnit.Centimeter), nameof(Length)), + new(LengthUnit.Kilometer,"Kilometers", new BaseUnits(LengthUnit.Kilometer), nameof(Length)) }; var expectedBaseUnit = LengthUnit.Centimeter; var expectedBaseDimensions = Length.BaseDimensions; @@ -204,8 +204,8 @@ public void GetUnitInfoFor_GivenBaseUnitsWithMultipleMatches_ThrowsInvalidOperat var quantityInfo = new QuantityInfo(Length.Info.Name, new UnitInfo[] { - new(LengthUnit.Meter, "Meters", baseUnits), - new(LengthUnit.Foot, "Feet", baseUnits) + new(LengthUnit.Meter, "Meters", baseUnits, nameof(Length)), + new(LengthUnit.Foot, "Feet", baseUnits, nameof(Length)) }, LengthUnit.Meter, Length.Zero, Length.BaseDimensions); @@ -242,8 +242,8 @@ public void GetUnitInfosFor_GivenBaseUnitsWithMultipleMatches_ReturnsMultipleMat var quantityInfo = new QuantityInfo(Length.Info.Name, new UnitInfo[] { - new(LengthUnit.Meter, "Meters", baseUnits), - new(LengthUnit.Foot, "Feet", baseUnits) }, + new(LengthUnit.Meter, "Meters", baseUnits, nameof(Length)), + new(LengthUnit.Foot, "Feet", baseUnits, nameof(Length)) }, LengthUnit.Meter, Length.Zero, Length.BaseDimensions); var result = quantityInfo.GetUnitInfosFor(baseUnits); diff --git a/UnitsNet.Tests/QuantityTypeConverterTest.cs b/UnitsNet.Tests/QuantityTypeConverterTest.cs index 199a754e9c..9f40f2d47f 100644 --- a/UnitsNet.Tests/QuantityTypeConverterTest.cs +++ b/UnitsNet.Tests/QuantityTypeConverterTest.cs @@ -132,7 +132,7 @@ public void ConvertFrom_GivenEmptyString_ThrowsNotSupportedException() } [Fact] - public void ConvertFrom_GivenWrongQuantity_ThrowsArgumentException() + public void ConvertFrom_GivenWrongQuantity_ThrowsUnitNotFoundException() { var converter = new QuantityTypeConverter(); ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", new Attribute[] { }); diff --git a/UnitsNet.Tests/UnitInfoTests.cs b/UnitsNet.Tests/UnitInfoTests.cs index 4efa6dcbd0..e05e3a51f9 100644 --- a/UnitsNet.Tests/UnitInfoTests.cs +++ b/UnitsNet.Tests/UnitInfoTests.cs @@ -11,7 +11,7 @@ public class UnitInfoTests [Fact] public void ConstructorTest() { - var unitInfo = new UnitInfo(LengthUnit.Meter, "Meters", new BaseUnits(LengthUnit.Meter)); + var unitInfo = new UnitInfo(LengthUnit.Meter, "Meters", new BaseUnits(LengthUnit.Meter), nameof(Length)); Assert.Equal(LengthUnit.Meter, unitInfo.Value); Assert.Equal(LengthUnit.Meter.ToString(), unitInfo.Name); } @@ -19,7 +19,7 @@ public void ConstructorTest() [Fact] public void GenericConstructorTest() { - var unitInfo = new UnitInfo(LengthUnit.Meter, "Meters", new BaseUnits(LengthUnit.Meter)); + var unitInfo = new UnitInfo(LengthUnit.Meter, "Meters", new BaseUnits(LengthUnit.Meter), nameof(Length)); Assert.Equal(LengthUnit.Meter, unitInfo.Value); Assert.Equal(LengthUnit.Meter.ToString(), unitInfo.Name); } diff --git a/UnitsNet/CustomCode/Quantity.cs b/UnitsNet/CustomCode/Quantity.cs index c9055717ce..ffbfc29071 100644 --- a/UnitsNet/CustomCode/Quantity.cs +++ b/UnitsNet/CustomCode/Quantity.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Globalization; +using System.Linq; using UnitsNet.Units; namespace UnitsNet @@ -40,7 +41,7 @@ public static bool TryGetUnitInfo(Enum unitEnum, [NotNullWhen(true)] out UnitInf Default.TryGetUnitInfo(unitEnum, out unitInfo); /// - /// + /// /// /// /// @@ -55,13 +56,153 @@ public static bool TryGetUnitInfo(Enum unitEnum, [NotNullWhen(true)] out UnitInf /// Unit value is not a known unit enum type. public static IQuantity From(QuantityValue value, Enum unit) { - return Default.From(value, unit); + return TryFrom(value, unit, out IQuantity? quantity) + ? quantity + : throw new UnitNotFoundException($"Unit value {unit} of type {unit.GetType()} is not a known unit enum type. Expected types like UnitsNet.Units.LengthUnit. Did you pass in a custom enum type defined outside the UnitsNet library?"); + } + + /// + /// 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. + /// Unit value is not a known unit enum type. + public static IQuantity From(QuantityValue value, string quantityName, string unitName) + { + // Get enum value for this unit, f.ex. LengthUnit.Meter for unit name "Meter". + return UnitConverter.TryParseUnit(quantityName, unitName, out Enum? unitValue) && + TryFrom(value, unitValue, out IQuantity? quantity) + ? quantity + : throw new UnitNotFoundException($"Unit [{unitName}] not found for quantity [{quantityName}]."); + } + + /// + /// 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(QuantityValue 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, QuantityValue value, string unitAbbreviation) + { + // TODO Optimize this with UnitValueAbbreviationLookup via UnitAbbreviationsCache.TryGetUnitValueAbbreviationLookup. + List units = GetUnitsForAbbreviation(formatProvider, unitAbbreviation); + if (units.Count > 1) + { + throw new AmbiguousUnitParseException($"Multiple units found matching the given unit abbreviation: {unitAbbreviation}"); + } + + if (units.Count == 0) + { + throw new UnitNotFoundException($"Unit abbreviation {unitAbbreviation} is not known. Did you pass in a custom unit abbreviation defined outside the UnitsNet library? This is currently not supported."); + } + + Enum unit = units.Single(); + return From(value, unit); } /// public static bool TryFrom(double value, Enum unit, [NotNullWhen(true)] out IQuantity? quantity) { - return Default.TryFrom(value, unit, out quantity); + quantity = default; + + // Implicit cast to QuantityValue would prevent TryFrom from being called, + // so we need to explicitly check this here for double arguments. + return !double.IsNaN(value) && + !double.IsInfinity(value) && + TryFrom((QuantityValue)value, unit, out quantity); + } + + /// + /// 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) + { + quantity = default; + + return UnitConverter.TryParseUnit(quantityName, unitName, out Enum? unitValue) && + TryFrom(value, unitValue, 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(QuantityValue 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, QuantityValue value, string unitAbbreviation, [NotNullWhen(true)] out IQuantity? quantity) + { + // TODO Optimize this with UnitValueAbbreviationLookup via UnitAbbreviationsCache.TryGetUnitValueAbbreviationLookup. + List units = GetUnitsForAbbreviation(formatProvider, unitAbbreviation); + if (units.Count == 1) + { + Enum? unit = units.SingleOrDefault(); + return TryFrom(value, unit, out quantity); + } + + quantity = default; + return false; } /// diff --git a/UnitsNet/GeneratedCode/Quantities/AbsorbedDoseOfIonizingRadiation.g.cs b/UnitsNet/GeneratedCode/Quantities/AbsorbedDoseOfIonizingRadiation.g.cs index 332ec19ee8..05442cf558 100644 --- a/UnitsNet/GeneratedCode/Quantities/AbsorbedDoseOfIonizingRadiation.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/AbsorbedDoseOfIonizingRadiation.g.cs @@ -68,22 +68,22 @@ static AbsorbedDoseOfIonizingRadiation() Info = new QuantityInfo("AbsorbedDoseOfIonizingRadiation", new UnitInfo[] { - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Centigray, "Centigrays", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Femtogray, "Femtograys", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Gigagray, "Gigagrays", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Gray, "Grays", new BaseUnits(length: LengthUnit.Meter, time: DurationUnit.Second)), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Kilogray, "Kilograys", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Kilorad, "Kilorads", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Megagray, "Megagrays", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Megarad, "Megarads", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Microgray, "Micrograys", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Milligray, "Milligrays", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Millirad, "Millirads", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Nanogray, "Nanograys", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Petagray, "Petagrays", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Picogray, "Picograys", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Rad, "Rads", BaseUnits.Undefined), - new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Teragray, "Teragrays", BaseUnits.Undefined), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Centigray, "Centigrays", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Femtogray, "Femtograys", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Gigagray, "Gigagrays", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Gray, "Grays", new BaseUnits(length: LengthUnit.Meter, time: DurationUnit.Second), "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Kilogray, "Kilograys", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Kilorad, "Kilorads", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Megagray, "Megagrays", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Megarad, "Megarads", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Microgray, "Micrograys", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Milligray, "Milligrays", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Millirad, "Millirads", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Nanogray, "Nanograys", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Petagray, "Petagrays", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Picogray, "Picograys", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Rad, "Rads", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), + new UnitInfo(AbsorbedDoseOfIonizingRadiationUnit.Teragray, "Teragrays", BaseUnits.Undefined, "AbsorbedDoseOfIonizingRadiation"), }, BaseUnit, Zero, BaseDimensions); @@ -315,42 +315,6 @@ internal static void RegisterDefaultConversions(UnitConverter unitConverter) unitConverter.SetConversionFunction(AbsorbedDoseOfIonizingRadiationUnit.Gray, AbsorbedDoseOfIonizingRadiationUnit.Teragray, quantity => quantity.ToUnit(AbsorbedDoseOfIonizingRadiationUnit.Teragray)); } - internal static void MapGeneratedLocalizations(UnitAbbreviationsCache unitAbbreviationsCache) - { - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Centigray, new CultureInfo("en-US"), false, true, new string[]{"cGy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Centigray, new CultureInfo("ru-RU"), false, true, new string[]{"сГр"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Femtogray, new CultureInfo("en-US"), false, true, new string[]{"fGy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Femtogray, new CultureInfo("ru-RU"), false, true, new string[]{"фГр"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Gigagray, new CultureInfo("en-US"), false, true, new string[]{"GGy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Gigagray, new CultureInfo("ru-RU"), false, true, new string[]{"ГГр"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Gray, new CultureInfo("en-US"), false, true, new string[]{"Gy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Gray, new CultureInfo("ru-RU"), false, true, new string[]{"Гр"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Kilogray, new CultureInfo("en-US"), false, true, new string[]{"kGy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Kilogray, new CultureInfo("ru-RU"), false, true, new string[]{"кГр"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Kilorad, new CultureInfo("en-US"), false, true, new string[]{"krad"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Kilorad, new CultureInfo("ru-RU"), false, true, new string[]{"крад"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Megagray, new CultureInfo("en-US"), false, true, new string[]{"MGy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Megagray, new CultureInfo("ru-RU"), false, true, new string[]{"МГр"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Megarad, new CultureInfo("en-US"), false, true, new string[]{"Mrad"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Megarad, new CultureInfo("ru-RU"), false, true, new string[]{"Мрад"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Microgray, new CultureInfo("en-US"), false, true, new string[]{"µGy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Microgray, new CultureInfo("ru-RU"), false, true, new string[]{"мкГр"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Milligray, new CultureInfo("en-US"), false, true, new string[]{"mGy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Milligray, new CultureInfo("ru-RU"), false, true, new string[]{"мГр"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Millirad, new CultureInfo("en-US"), false, true, new string[]{"mrad"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Millirad, new CultureInfo("ru-RU"), false, true, new string[]{"мрад"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Nanogray, new CultureInfo("en-US"), false, true, new string[]{"nGy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Nanogray, new CultureInfo("ru-RU"), false, true, new string[]{"нГр"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Petagray, new CultureInfo("en-US"), false, true, new string[]{"PGy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Petagray, new CultureInfo("ru-RU"), false, true, new string[]{"ПГр"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Picogray, new CultureInfo("en-US"), false, true, new string[]{"pGy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Picogray, new CultureInfo("ru-RU"), false, true, new string[]{"пГр"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Rad, new CultureInfo("en-US"), false, true, new string[]{"rad"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Rad, new CultureInfo("ru-RU"), false, true, new string[]{"рад"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Teragray, new CultureInfo("en-US"), false, true, new string[]{"TGy"}); - unitAbbreviationsCache.PerformAbbreviationMapping(AbsorbedDoseOfIonizingRadiationUnit.Teragray, new CultureInfo("ru-RU"), false, true, new string[]{"ТГр"}); - } - /// /// Get unit abbreviation string. /// diff --git a/UnitsNet/GeneratedCode/Quantities/Volume.g.cs b/UnitsNet/GeneratedCode/Quantities/Volume.g.cs index 6f347459ed..0f4a7114fd 100644 --- a/UnitsNet/GeneratedCode/Quantities/Volume.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Volume.g.cs @@ -92,6 +92,7 @@ static Volume() new UnitInfo(VolumeUnit.ImperialGallon, "ImperialGallons", BaseUnits.Undefined, "Volume"), new UnitInfo(VolumeUnit.ImperialOunce, "ImperialOunces", BaseUnits.Undefined, "Volume"), new UnitInfo(VolumeUnit.ImperialPint, "ImperialPints", new BaseUnits(length: LengthUnit.Decimeter), "Volume"), + new UnitInfo(VolumeUnit.ImperialQuart, "ImperialQuarts", BaseUnits.Undefined, "Volume"), new UnitInfo(VolumeUnit.KilocubicFoot, "KilocubicFeet", BaseUnits.Undefined, "Volume"), new UnitInfo(VolumeUnit.KilocubicMeter, "KilocubicMeters", BaseUnits.Undefined, "Volume"), new UnitInfo(VolumeUnit.KiloimperialGallon, "KiloimperialGallons", BaseUnits.Undefined, "Volume"), diff --git a/UnitsNet/GeneratedCode/Quantity.g.cs b/UnitsNet/GeneratedCode/Quantity.g.cs index 1ce23e286b..5236f4b15a 100644 --- a/UnitsNet/GeneratedCode/Quantity.g.cs +++ b/UnitsNet/GeneratedCode/Quantity.g.cs @@ -305,6 +305,7 @@ public static bool TryFrom(QuantityValue value, Enum? unit, [NotNullWhen(true)] { quantity = unit switch { + AbsorbedDoseOfIonizingRadiationUnit absorbedDoseOfIonizingRadiationUnit => AbsorbedDoseOfIonizingRadiation.From(value, absorbedDoseOfIonizingRadiationUnit), AccelerationUnit accelerationUnit => Acceleration.From(value, accelerationUnit), AmountOfSubstanceUnit amountOfSubstanceUnit => AmountOfSubstance.From(value, amountOfSubstanceUnit), AmplitudeRatioUnit amplitudeRatioUnit => AmplitudeRatio.From(value, amplitudeRatioUnit), diff --git a/UnitsNet/GeneratedCode/Resources/AbsorbedDoseOfIonizingRadiation.restext b/UnitsNet/GeneratedCode/Resources/AbsorbedDoseOfIonizingRadiation.restext new file mode 100644 index 0000000000..466e136996 --- /dev/null +++ b/UnitsNet/GeneratedCode/Resources/AbsorbedDoseOfIonizingRadiation.restext @@ -0,0 +1,16 @@ +Centigrays=cGy +Femtograys=fGy +Gigagrays=GGy +Grays=Gy +Kilograys=kGy +Kilorads=krad +Megagrays=MGy +Megarads=Mrad +Micrograys=µGy +Milligrays=mGy +Millirads=mrad +Nanograys=nGy +Petagrays=PGy +Picograys=pGy +Rads=rad +Teragrays=TGy diff --git a/UnitsNet/GeneratedCode/Resources/AbsorbedDoseOfIonizingRadiation.ru-RU.restext b/UnitsNet/GeneratedCode/Resources/AbsorbedDoseOfIonizingRadiation.ru-RU.restext new file mode 100644 index 0000000000..f841f92bbc --- /dev/null +++ b/UnitsNet/GeneratedCode/Resources/AbsorbedDoseOfIonizingRadiation.ru-RU.restext @@ -0,0 +1,16 @@ +Centigrays=сГр +Femtograys=фГр +Gigagrays=ГГр +Grays=Гр +Kilograys=кГр +Kilorads=крад +Megagrays=МГр +Megarads=Мрад +Micrograys=мкГр +Milligrays=мГр +Millirads=мрад +Nanograys=нГр +Petagrays=ПГр +Picograys=пГр +Rads=рад +Teragrays=ТГр diff --git a/UnitsNet/GeneratedCode/Resources/Volume.restext b/UnitsNet/GeneratedCode/Resources/Volume.restext index 91d61b934e..b88e0a84b1 100644 --- a/UnitsNet/GeneratedCode/Resources/Volume.restext +++ b/UnitsNet/GeneratedCode/Resources/Volume.restext @@ -24,6 +24,7 @@ ImperialBeerBarrels=bl (imp.) ImperialGallons=gal (imp.) ImperialOunces=oz (imp.) ImperialPints=pt (imp.),UK pt,pt,p +ImperialQuarts=qt (imp.) KilocubicFeet=kft³ KilocubicMeters=km³ KiloimperialGallons=kgal (imp.) diff --git a/UnitsNet/QuantityInfoLookup.cs b/UnitsNet/QuantityInfoLookup.cs index 0aed43177b..0785497e3f 100644 --- a/UnitsNet/QuantityInfoLookup.cs +++ b/UnitsNet/QuantityInfoLookup.cs @@ -59,7 +59,7 @@ public bool TryGetUnitInfo(Enum unitEnum, [NotNullWhen(true)] out UnitInfo? unit UnitTypeAndNameToUnitInfoLazy.Value.TryGetValue((unitEnum.GetType(), unitEnum.ToString()), out unitInfo); /// - /// + /// /// /// /// @@ -77,11 +77,9 @@ public void AddUnitInfo(Enum unit, UnitInfo unitInfo) /// Unit value is not a know unit enum type. public IQuantity From(QuantityValue value, Enum unit) { - if (Quantity.TryFrom(value, unit, out IQuantity? quantity)) - return quantity; - - throw new ArgumentException( - $"Unit value {unit} of type {unit.GetType()} is not a known unit enum type. Expected types like UnitsNet.Units.LengthUnit. Did you pass in a third-party enum type defined outside UnitsNet library?"); + return Quantity.TryFrom(value, unit, out IQuantity? quantity) + ? quantity + : throw new UnitNotFoundException($"Unit value {unit} of type {unit.GetType()} is not a known unit enum type. Expected types like UnitsNet.Units.LengthUnit. Did you pass in a custom enum type defined outside the UnitsNet library?"); } /// @@ -117,7 +115,7 @@ public IQuantity Parse(IFormatProvider? formatProvider, Type quantityType, strin if (Quantity.TryParse(formatProvider, quantityType, quantityString, out IQuantity? quantity)) return quantity; - throw new ArgumentException($"Quantity string could not be parsed to quantity {quantityType}."); + throw new UnitNotFoundException($"Quantity string '{quantityString}' could not be parsed to quantity '{quantityType}'."); } ///