diff --git a/UnitsNet.Tests/CustomCode/IQuantityTests.cs b/UnitsNet.Tests/CustomCode/IQuantityTests.cs index ce591ef0aa..c6011b07c9 100644 --- a/UnitsNet.Tests/CustomCode/IQuantityTests.cs +++ b/UnitsNet.Tests/CustomCode/IQuantityTests.cs @@ -59,6 +59,48 @@ 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_Zero_ReturnsBaseUnitInfo() + { + IQuantity quantity = Length.Info.Zero; + + UnitInfo unitInfo = quantity.UnitInfo; + + 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 1c0c45f05e..6a4e258f7f 100644 --- a/UnitsNet/Extensions/QuantityExtensions.cs +++ b/UnitsNet/Extensions/QuantityExtensions.cs @@ -11,6 +11,38 @@ 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. + /// + /// 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]; + } + + /// + /// 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 + /// /// 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..bb49466d68 100644 --- a/UnitsNet/IQuantity.cs +++ b/UnitsNet/IQuantity.cs @@ -58,6 +58,17 @@ 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. + /// + /// + /// 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 } /// @@ -94,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;