Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions UnitsNet.Tests/CustomCode/IQuantityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LengthUnit> quantity = new Length(3.0, LengthUnit.Centimeter);

UnitInfo<LengthUnit> 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()
{
Expand Down
32 changes: 32 additions & 0 deletions UnitsNet/Extensions/QuantityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,38 @@ namespace UnitsNet;
/// </summary>
public static class QuantityExtensions
{
#if !NET
/// <summary>
/// Gets the <see cref="UnitInfo"/> for the unit this quantity was constructed with.
/// </summary>
/// <param name="quantity">The quantity.</param>
/// <returns>The <see cref="UnitInfo"/> for the quantity's unit.</returns>
/// <remarks>
/// On .NET 5+ targets, this is available as a default interface member property
/// <c>IQuantity.UnitInfo</c> instead.
/// </remarks>
public static UnitInfo GetUnitInfo(this IQuantity quantity)
{
return quantity.QuantityInfo[quantity.UnitKey];
}

/// <summary>
/// Gets the <see cref="UnitInfo{TUnit}"/> for the unit this quantity was constructed with.
/// </summary>
/// <typeparam name="TUnit">The unit enum type.</typeparam>
/// <param name="quantity">The quantity.</param>
/// <returns>The <see cref="UnitInfo{TUnit}"/> for the quantity's unit.</returns>
/// <remarks>
/// On .NET 5+ targets, this is available as a default interface member property
/// <c>IQuantity&lt;TUnitType&gt;.UnitInfo</c> instead.
/// </remarks>
public static UnitInfo<TUnit> GetUnitInfo<TUnit>(this IQuantity<TUnit> quantity)
where TUnit : struct, Enum
{
return quantity.QuantityInfo[quantity.Unit];
}
#endif

/// <inheritdoc cref="IQuantity.As(UnitKey)" />
/// <remarks>This should be using UnitConverter.Default.ConvertValue(quantity, toUnit) </remarks>
internal static double GetValue<TQuantity>(this TQuantity quantity, UnitKey toUnit)
Expand Down
16 changes: 16 additions & 0 deletions UnitsNet/IQuantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ public interface IQuantity : IFormattable
/// as it avoids the boxing that would normally occur when casting the enum to <see cref="Enum" />.
/// </remarks>
UnitKey UnitKey { get; }

#if NET
/// <summary>
/// Gets the <see cref="UnitsNet.UnitInfo"/> for the unit this quantity was constructed with.
/// </summary>
/// <remarks>
/// On targets that do not support default interface members (e.g. netstandard2.0),
/// use the <c>GetUnitInfo()</c> extension method from <see cref="QuantityExtensions"/> instead.
/// </remarks>
UnitInfo UnitInfo => QuantityInfo[UnitKey];
#endif
}

/// <summary>
Expand Down Expand Up @@ -94,8 +105,13 @@ public interface IQuantity<TUnitType> : IQuantity

#if NET

/// <inheritdoc cref="IQuantity.UnitInfo"/>
new UnitInfo<TUnitType> UnitInfo => QuantityInfo[Unit];

#region Implementation of IQuantity

UnitInfo IQuantity.UnitInfo => UnitInfo;

QuantityInfo IQuantity.QuantityInfo
{
get => QuantityInfo;
Expand Down
Loading