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
33 changes: 24 additions & 9 deletions UnitsNet.Tests/CustomCode/IQuantityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,44 +60,59 @@ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
}

[Fact]
public void UnitInfo_ReturnsUnitInfoForQuantityUnit()
public void GetUnitInfo_ReturnsUnitInfoForQuantityUnit()
{
var length = new Length(3.0, LengthUnit.Centimeter);
IQuantity quantity = length;

UnitInfo unitInfo = quantity.UnitInfo;
UnitInfo unitInfo = quantity.GetUnitInfo();

Assert.Equal(nameof(LengthUnit.Centimeter), unitInfo.Name);
Assert.Equal(quantity.UnitKey, unitInfo.UnitKey);
}

[Fact]
public void UnitInfo_Zero_ReturnsBaseUnitInfo()
public void GetUnitInfo_Zero_ReturnsBaseUnitInfo()
{
IQuantity quantity = Length.Info.Zero;

UnitInfo unitInfo = quantity.UnitInfo;
UnitInfo unitInfo = quantity.GetUnitInfo();

Assert.Equal(Length.Info.BaseUnitInfo.UnitKey, unitInfo.UnitKey);
}

[Fact]
public void UnitInfo_TypedQuantity_ReturnsTypedUnitInfo()
public void GetUnitInfo_ConcreteQuantity_ReturnsFullyTypedUnitInfo()
{
IQuantity<LengthUnit> quantity = new Length(3.0, LengthUnit.Centimeter);
var quantity = new Length(3.0, LengthUnit.Centimeter);

UnitInfo<LengthUnit> unitInfo = quantity.UnitInfo;
// Overload resolution picks GetUnitInfo<TQuantity, TUnit> for the concrete struct receiver,
// returning the most specific UnitInfo<Length, LengthUnit>.
UnitInfo<Length, LengthUnit> unitInfo = quantity.GetUnitInfo();

Assert.Equal(LengthUnit.Centimeter, unitInfo.Value);
Assert.Equal(nameof(LengthUnit.Centimeter), unitInfo.Name);
}

[Fact]
public void UnitInfo_MatchesUnit()
public void GetUnitInfo_TypedQuantityReference_FallsBackToNonGeneric()
{
IQuantity<LengthUnit> quantity = new Length(3.0, LengthUnit.Centimeter);

// The IQuantity<TUnit> reference does not satisfy the IQuantity<TSelf, TUnit> constraint
// (TSelf would be IQuantity<MassUnit>), so resolution falls back to GetUnitInfo(IQuantity).
UnitInfo unitInfo = quantity.GetUnitInfo();

Assert.Equal(LengthUnit.Centimeter, ((UnitInfo<LengthUnit>)unitInfo).Value);
Assert.Equal(nameof(LengthUnit.Centimeter), unitInfo.Name);
}

[Fact]
public void GetUnitInfo_MatchesUnit()
{
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity =>
{
Assert.Equal(quantity.Unit, quantity.UnitInfo.Value);
Assert.Equal(quantity.Unit, quantity.GetUnitInfo().Value);
});
}

Expand Down
30 changes: 17 additions & 13 deletions UnitsNet/Extensions/QuantityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,41 @@ 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.
/// Picked by overload resolution for callers that only have an <see cref="IQuantity"/> reference.
/// Concretely-typed callers (e.g. a <c>Mass</c> receiver) bind to the
/// <see cref="GetUnitInfo{TQuantity,TUnit}(IQuantity{TQuantity,TUnit})"/> overload and get the
/// more specific <see cref="UnitInfo{TQuantity,TUnit}"/> return.
/// </remarks>
/// <param name="quantity">The quantity.</param>
/// <returns>The <see cref="UnitInfo"/> for the quantity's unit.</returns>
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.
/// Gets the <see cref="UnitInfo{TQuantity,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.
/// Picked by overload resolution for concretely-typed receivers (e.g. <c>Mass</c>) where C# can
/// infer both <typeparamref name="TQuantity"/> and <typeparamref name="TUnit"/> from the receiver's
/// <see cref="IQuantity{TSelf,TUnit}"/> implementation. Callers with only an <see cref="IQuantity"/>
/// reference fall back to the non-generic <see cref="GetUnitInfo(IQuantity)"/> overload.
/// </remarks>
public static UnitInfo<TUnit> GetUnitInfo<TUnit>(this IQuantity<TUnit> quantity)
/// <typeparam name="TQuantity">The quantity type.</typeparam>
/// <typeparam name="TUnit">The unit enum type.</typeparam>
/// <param name="quantity">The quantity.</param>
/// <returns>The <see cref="UnitInfo{TQuantity,TUnit}"/> for the quantity's unit.</returns>
public static UnitInfo<TQuantity, TUnit> GetUnitInfo<TQuantity, TUnit>(this IQuantity<TQuantity, TUnit> quantity)
where TQuantity : IQuantity<TQuantity, TUnit>
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>
Expand Down
16 changes: 0 additions & 16 deletions UnitsNet/IQuantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,6 @@ 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 @@ -105,13 +94,8 @@ 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