From 4da9f57acc58b36adaa04def5906b0deb38e53ed Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Mon, 6 Apr 2026 13:59:05 +0200 Subject: [PATCH 1/5] Add IQuantity.UnitInfo convenience property Adds a way to get UnitInfo directly from a quantity instance, avoiding the verbose `quantity.QuantityInfo[quantity.UnitKey]` pattern. On net8.0+ this is a default interface member (`quantity.UnitInfo`). On netstandard2.0 it is an extension method (`quantity.GetUnitInfo()`). Co-Authored-By: Claude Opus 4.6 (1M context) --- UnitsNet/Extensions/QuantityExtensions.cs | 12 ++++++++++++ UnitsNet/IQuantity.cs | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/UnitsNet/Extensions/QuantityExtensions.cs b/UnitsNet/Extensions/QuantityExtensions.cs index 1c0c45f05e..67d8126d06 100644 --- a/UnitsNet/Extensions/QuantityExtensions.cs +++ b/UnitsNet/Extensions/QuantityExtensions.cs @@ -11,6 +11,18 @@ namespace UnitsNet; /// public static class QuantityExtensions { +#if !NET + /// + /// Gets the for the unit this quantity was constructed with. + /// + /// The quantity. + /// The for the quantity's unit. + public static UnitInfo GetUnitInfo(this IQuantity quantity) + { + return quantity.QuantityInfo[quantity.UnitKey]; + } +#endif + /// /// This should be using UnitConverter.Default.ConvertValue(quantity, toUnit) internal static double GetValue(this TQuantity quantity, UnitKey toUnit) diff --git a/UnitsNet/IQuantity.cs b/UnitsNet/IQuantity.cs index 149b3f9dd0..eadeb96913 100644 --- a/UnitsNet/IQuantity.cs +++ b/UnitsNet/IQuantity.cs @@ -58,6 +58,13 @@ public interface IQuantity : IFormattable /// as it avoids the boxing that would normally occur when casting the enum to . /// UnitKey UnitKey { get; } + +#if NET + /// + /// Gets the for the unit this quantity was constructed with. + /// + UnitInfo UnitInfo => QuantityInfo[UnitKey]; +#endif } /// From 0ad42dbcd7c02ec2acc3243b8be4e2811d13620d Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Mon, 6 Apr 2026 14:04:10 +0200 Subject: [PATCH 2/5] Add tests for IQuantity.UnitInfo property Co-Authored-By: Claude Opus 4.6 (1M context) --- UnitsNet.Tests/CustomCode/IQuantityTests.cs | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/UnitsNet.Tests/CustomCode/IQuantityTests.cs b/UnitsNet.Tests/CustomCode/IQuantityTests.cs index ce591ef0aa..bb1f85432b 100644 --- a/UnitsNet.Tests/CustomCode/IQuantityTests.cs +++ b/UnitsNet.Tests/CustomCode/IQuantityTests.cs @@ -59,6 +59,28 @@ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull() }); } + [Fact] + public void UnitInfo_ReturnsUnitInfoForQuantityUnit() + { + var length = new Length(3.0, LengthUnit.Centimeter); + IQuantity quantity = length; + + UnitInfo unitInfo = quantity.UnitInfo; + + Assert.Equal(nameof(LengthUnit.Centimeter), unitInfo.Name); + Assert.Equal(quantity.UnitKey, unitInfo.UnitKey); + } + + [Fact] + public void UnitInfo_ReturnsBaseUnitInfo_WhenDefaultConstructed() + { + IQuantity quantity = new Length(1.0, Length.BaseUnit); + + UnitInfo unitInfo = quantity.UnitInfo; + + Assert.Equal(Length.Info.BaseUnitInfo.UnitKey, unitInfo.UnitKey); + } + [Fact] public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported() { From 520759d1c01ee4efb0fab42a506de30cec25e25f Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Mon, 6 Apr 2026 14:06:57 +0200 Subject: [PATCH 3/5] Address PR review: typed variant, docs, and more tests - Add typed UnitInfo on IQuantity for strongly-typed access - Add cross-referencing the property and extension method - Add test for typed IQuantity.UnitInfo - Add test asserting UnitInfo.Value matches Unit across all quantities Co-Authored-By: Claude Opus 4.6 (1M context) --- UnitsNet.Tests/CustomCode/IQuantityTests.cs | 20 ++++++++++++++++++++ UnitsNet/Extensions/QuantityExtensions.cs | 4 ++++ UnitsNet/IQuantity.cs | 9 +++++++++ 3 files changed, 33 insertions(+) diff --git a/UnitsNet.Tests/CustomCode/IQuantityTests.cs b/UnitsNet.Tests/CustomCode/IQuantityTests.cs index bb1f85432b..d15e9c566f 100644 --- a/UnitsNet.Tests/CustomCode/IQuantityTests.cs +++ b/UnitsNet.Tests/CustomCode/IQuantityTests.cs @@ -81,6 +81,26 @@ public void UnitInfo_ReturnsBaseUnitInfo_WhenDefaultConstructed() Assert.Equal(Length.Info.BaseUnitInfo.UnitKey, unitInfo.UnitKey); } + [Fact] + public void UnitInfo_TypedQuantity_ReturnsTypedUnitInfo() + { + IQuantity quantity = new Length(3.0, LengthUnit.Centimeter); + + UnitInfo unitInfo = quantity.UnitInfo; + + Assert.Equal(LengthUnit.Centimeter, unitInfo.Value); + Assert.Equal(nameof(LengthUnit.Centimeter), unitInfo.Name); + } + + [Fact] + public void UnitInfo_MatchesUnit() + { + Assert.All(Quantity.Infos.Select(x => x.Zero), quantity => + { + Assert.Equal(quantity.Unit, quantity.UnitInfo.Value); + }); + } + [Fact] public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported() { diff --git a/UnitsNet/Extensions/QuantityExtensions.cs b/UnitsNet/Extensions/QuantityExtensions.cs index 67d8126d06..2fd1b0e7d7 100644 --- a/UnitsNet/Extensions/QuantityExtensions.cs +++ b/UnitsNet/Extensions/QuantityExtensions.cs @@ -17,6 +17,10 @@ public static class QuantityExtensions /// /// The quantity. /// The for the quantity's unit. + /// + /// On .NET 5+ targets, this is available as a default interface member property + /// IQuantity.UnitInfo instead. + /// public static UnitInfo GetUnitInfo(this IQuantity quantity) { return quantity.QuantityInfo[quantity.UnitKey]; diff --git a/UnitsNet/IQuantity.cs b/UnitsNet/IQuantity.cs index eadeb96913..bb49466d68 100644 --- a/UnitsNet/IQuantity.cs +++ b/UnitsNet/IQuantity.cs @@ -63,6 +63,10 @@ public interface IQuantity : IFormattable /// /// Gets the for the unit this quantity was constructed with. /// + /// + /// On targets that do not support default interface members (e.g. netstandard2.0), + /// use the GetUnitInfo() extension method from instead. + /// UnitInfo UnitInfo => QuantityInfo[UnitKey]; #endif } @@ -101,8 +105,13 @@ public interface IQuantity : IQuantity #if NET + /// + new UnitInfo UnitInfo => QuantityInfo[Unit]; + #region Implementation of IQuantity + UnitInfo IQuantity.UnitInfo => UnitInfo; + QuantityInfo IQuantity.QuantityInfo { get => QuantityInfo; From 730b04cfbf617766e17000710c323bcaf6472efb Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Mon, 6 Apr 2026 14:16:34 +0200 Subject: [PATCH 4/5] Replace default-constructed test with Zero instance test Co-Authored-By: Claude Opus 4.6 (1M context) --- UnitsNet.Tests/CustomCode/IQuantityTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UnitsNet.Tests/CustomCode/IQuantityTests.cs b/UnitsNet.Tests/CustomCode/IQuantityTests.cs index d15e9c566f..c6011b07c9 100644 --- a/UnitsNet.Tests/CustomCode/IQuantityTests.cs +++ b/UnitsNet.Tests/CustomCode/IQuantityTests.cs @@ -72,9 +72,9 @@ public void UnitInfo_ReturnsUnitInfoForQuantityUnit() } [Fact] - public void UnitInfo_ReturnsBaseUnitInfo_WhenDefaultConstructed() + public void UnitInfo_Zero_ReturnsBaseUnitInfo() { - IQuantity quantity = new Length(1.0, Length.BaseUnit); + IQuantity quantity = Length.Info.Zero; UnitInfo unitInfo = quantity.UnitInfo; From 716bca930ddc815c0b54d0cf70d41fd4806f8f93 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Mon, 6 Apr 2026 14:38:21 +0200 Subject: [PATCH 5/5] Add typed GetUnitInfo extension for netstandard2.0 parity Co-Authored-By: Claude Opus 4.6 (1M context) --- UnitsNet/Extensions/QuantityExtensions.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/UnitsNet/Extensions/QuantityExtensions.cs b/UnitsNet/Extensions/QuantityExtensions.cs index 2fd1b0e7d7..6a4e258f7f 100644 --- a/UnitsNet/Extensions/QuantityExtensions.cs +++ b/UnitsNet/Extensions/QuantityExtensions.cs @@ -25,6 +25,22 @@ public static UnitInfo GetUnitInfo(this IQuantity quantity) { return quantity.QuantityInfo[quantity.UnitKey]; } + + /// + /// Gets the for the unit this quantity was constructed with. + /// + /// The unit enum type. + /// The quantity. + /// The for the quantity's unit. + /// + /// On .NET 5+ targets, this is available as a default interface member property + /// IQuantity<TUnitType>.UnitInfo instead. + /// + public static UnitInfo GetUnitInfo(this IQuantity quantity) + where TUnit : struct, Enum + { + return quantity.QuantityInfo[quantity.Unit]; + } #endif ///