From abcdb720c12fc32b270f3d4fa1c31ae79b25c702 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Fri, 25 Jul 2025 18:59:34 +0200 Subject: [PATCH 1/3] refactor: change DefaultQuantities to use property initializer --- UnitsNet/GeneratedCode/Quantity.g.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitsNet/GeneratedCode/Quantity.g.cs b/UnitsNet/GeneratedCode/Quantity.g.cs index 403d455522..1786f3e121 100644 --- a/UnitsNet/GeneratedCode/Quantity.g.cs +++ b/UnitsNet/GeneratedCode/Quantity.g.cs @@ -34,7 +34,7 @@ internal static class Provider /// /// All QuantityInfo instances that are present in UnitsNet by default. /// - internal static IReadOnlyList DefaultQuantities => new QuantityInfo[] + internal static IReadOnlyList DefaultQuantities { get; } = new QuantityInfo[] { AbsorbedDoseOfIonizingRadiation.Info, Acceleration.Info, From 4a426140e2859f7379be1352506f1e73fdc246ad Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Fri, 25 Jul 2025 19:21:18 +0200 Subject: [PATCH 2/3] refactor: rename to Quantity.DefaultProvider.Quantities --- CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs | 8 ++++---- UnitsNet/CustomCode/UnitsNetSetup.cs | 7 ++++--- UnitsNet/GeneratedCode/Quantity.g.cs | 8 ++++---- UnitsNet/QuantityInfoLookup.cs | 8 ++++---- UnitsNet/UnitConverter.cs | 4 ++-- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs b/CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs index 04cb812d54..f25cae0c6e 100644 --- a/CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs +++ b/CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs @@ -28,18 +28,18 @@ public partial class Quantity /// /// Serves as a repository for predefined quantity conversion mappings, facilitating the automatic generation and retrieval of unit conversions in the UnitsNet library. /// - internal static class Provider + internal static class DefaultProvider { /// /// All QuantityInfo instances that are present in UnitsNet by default. /// - internal static IReadOnlyList DefaultQuantities => new QuantityInfo[] - {"); + internal static IReadOnlyList Quantities { get; } = + ["); foreach (var quantity in _quantities) Writer.WL($@" {quantity.Name}.Info,"); Writer.WL(@" - }; + ]; } }"); return Writer.ToString(); diff --git a/UnitsNet/CustomCode/UnitsNetSetup.cs b/UnitsNet/CustomCode/UnitsNetSetup.cs index cb45d2063b..ed53a93d5d 100644 --- a/UnitsNet/CustomCode/UnitsNetSetup.cs +++ b/UnitsNet/CustomCode/UnitsNetSetup.cs @@ -21,7 +21,8 @@ public sealed class UnitsNetSetup { static UnitsNetSetup() { - IReadOnlyCollection quantityInfos = Quantity.Provider.DefaultQuantities; + IReadOnlyCollection quantityInfos = Quantity.DefaultProvider.Quantities; + // note: in order to support the ConvertByAbbreviation, the unit converter should require a UnitParser in the constructor var unitConverter = UnitConverter.CreateDefault(); @@ -37,7 +38,7 @@ public UnitsNetSetup(IEnumerable quantityInfos, UnitConverter unit { var quantityInfoLookup = new QuantityInfoLookup(quantityInfos); var unitAbbreviations = new UnitAbbreviationsCache(quantityInfoLookup); - + UnitConverter = unitConverter; UnitAbbreviations = unitAbbreviations; Formatter = new QuantityFormatter(unitAbbreviations); @@ -68,7 +69,7 @@ public UnitsNetSetup(IEnumerable quantityInfos, UnitConverter unit /// quantities. /// public UnitAbbreviationsCache UnitAbbreviations { get; } - + /// /// Converts a quantity to string using the specified format strings and culture-specific format providers. /// diff --git a/UnitsNet/GeneratedCode/Quantity.g.cs b/UnitsNet/GeneratedCode/Quantity.g.cs index 1786f3e121..ee686fea09 100644 --- a/UnitsNet/GeneratedCode/Quantity.g.cs +++ b/UnitsNet/GeneratedCode/Quantity.g.cs @@ -29,13 +29,13 @@ public partial class Quantity /// /// Serves as a repository for predefined quantity conversion mappings, facilitating the automatic generation and retrieval of unit conversions in the UnitsNet library. /// - internal static class Provider + internal static class DefaultProvider { /// /// All QuantityInfo instances that are present in UnitsNet by default. /// - internal static IReadOnlyList DefaultQuantities { get; } = new QuantityInfo[] - { + internal static IReadOnlyList Quantities { get; } = + [ AbsorbedDoseOfIonizingRadiation.Info, Acceleration.Info, AmountOfSubstance.Info, @@ -163,6 +163,6 @@ internal static class Provider VolumePerLength.Info, VolumetricHeatCapacity.Info, WarpingMomentOfInertia.Info, - }; + ]; } } diff --git a/UnitsNet/QuantityInfoLookup.cs b/UnitsNet/QuantityInfoLookup.cs index 8a9dc0a6c2..b3bae28117 100644 --- a/UnitsNet/QuantityInfoLookup.cs +++ b/UnitsNet/QuantityInfoLookup.cs @@ -23,7 +23,7 @@ namespace UnitsNet; /// internal class QuantityInfoLookup { - private readonly QuantityInfo[] _quantities; + private readonly IReadOnlyList _quantities; private readonly Lazy _quantitiesByName; private readonly Lazy _quantitiesByType; private readonly Lazy _quantitiesByUnitType; @@ -174,7 +174,7 @@ public IQuantity From(double value, UnitKey unit) { return GetUnitInfo(unit).From(value); } - + /// /// Attempts to create a quantity from the specified value and unit. /// @@ -194,7 +194,7 @@ public bool TryFrom(double value, [NotNullWhen(true)] Enum? unit, [NotNullWhen(t quantity = null; return false; } - + if (!TryGetUnitInfo(unit, out UnitInfo? unitInfo)) { quantity = null; @@ -270,7 +270,7 @@ public UnitInfo GetUnitByName(string quantityName, string unitName) Data = { ["quantityName"] = quantityName, ["unitName"] = unitName } }; } - + /// /// Attempts to parse unit information based on its quantity and unit names. /// diff --git a/UnitsNet/UnitConverter.cs b/UnitsNet/UnitConverter.cs index 76f7fdff17..0a9b75df11 100644 --- a/UnitsNet/UnitConverter.cs +++ b/UnitsNet/UnitConverter.cs @@ -85,7 +85,7 @@ public static void RegisterDefaultConversions(UnitConverter unitConverter) if (unitConverter is null) throw new ArgumentNullException(nameof(unitConverter)); - foreach(var quantity in Quantity.Provider.DefaultQuantities) + foreach (var quantity in Quantity.DefaultProvider.Quantities) { var registerMethod = quantity.QuantityType.GetMethod(nameof(Length.RegisterDefaultConversions), BindingFlags.NonPublic | BindingFlags.Static); registerMethod?.Invoke(null, new object[]{unitConverter}); @@ -369,7 +369,7 @@ public static bool TryConvertByName(double inputValue, string quantityName, stri result = quantity.As(toUnitInfo.Value); return true; } - + result = 0d; return false; } From b44cd4ca00bb577f179097ce455754c63fce708f Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Fri, 25 Jul 2025 20:47:32 +0200 Subject: [PATCH 3/3] revert back to array, conditional ToArray() --- UnitsNet/QuantityInfoLookup.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/UnitsNet/QuantityInfoLookup.cs b/UnitsNet/QuantityInfoLookup.cs index b3bae28117..1a8b9bb233 100644 --- a/UnitsNet/QuantityInfoLookup.cs +++ b/UnitsNet/QuantityInfoLookup.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Linq; +using System.Linq; #if NET8_0_OR_GREATER using System.Collections.Frozen; using QuantityByTypeLookupDictionary = System.Collections.Frozen.FrozenDictionary; @@ -23,7 +20,7 @@ namespace UnitsNet; /// internal class QuantityInfoLookup { - private readonly IReadOnlyList _quantities; + private readonly QuantityInfo[] _quantities; private readonly Lazy _quantitiesByName; private readonly Lazy _quantitiesByType; private readonly Lazy _quantitiesByUnitType; @@ -80,7 +77,7 @@ private UnitByKeyLookupDictionary GroupUnitsByKey() /// public QuantityInfoLookup(IEnumerable quantityInfos) { - _quantities = quantityInfos.ToArray(); + _quantities = quantityInfos as QuantityInfo[] ?? quantityInfos.ToArray(); _quantitiesByName = new Lazy(GroupQuantitiesByName); _quantitiesByType = new Lazy(GroupQuantitiesByType); _quantitiesByUnitType = new Lazy(GroupQuantitiesByUnitType);