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
45 changes: 45 additions & 0 deletions UnitsNet.Tests/UnitInfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com).
// https://github.com/angularsen/UnitsNet
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using UnitsNet.Units;
using Xunit;

namespace UnitsNet.Tests
{
public class UnitInfoTests
{
[Fact]
public void ConstructorTest()
{
var unitInfo = new UnitInfo(LengthUnit.Meter);
Assert.Equal(LengthUnit.Meter, unitInfo.Value);
Assert.Equal(LengthUnit.Meter.ToString(), unitInfo.Name);
}

[Fact]
public void GenericConstructorTest()
{
var unitInfo = new UnitInfo<LengthUnit>(LengthUnit.Meter);
Assert.Equal(LengthUnit.Meter, unitInfo.Value);
Assert.Equal(LengthUnit.Meter.ToString(), unitInfo.Name);
}
}
}
26 changes: 23 additions & 3 deletions UnitsNet/QuantityInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public QuantityInfo(QuantityType quantityType, [NotNull] Enum[] units, [NotNull]
Name = quantityType.ToString();
QuantityType = quantityType;
UnitType = UnitEnumTypes.First(t => t.Name == $"{quantityType}Unit");
UnitNames = units.Select(u => u.ToString()).ToArray();
UnitInfos = units.Select(unit => new UnitInfo(unit)).ToArray();
UnitNames = UnitInfos.Select(unitInfo => unitInfo.Name).ToArray();
Units = units;
BaseUnit = baseUnit;
BaseUnitInfo = new UnitInfo(baseUnit);
BaseUnit = BaseUnitInfo.Value;
Zero = zero;
ValueType = zero.GetType();
BaseDimensions = baseDimensions;
Expand All @@ -82,20 +84,27 @@ public QuantityInfo(QuantityType quantityType, [NotNull] Enum[] units, [NotNull]
/// </summary>
public QuantityType QuantityType { get; }

public UnitInfo[] UnitInfos { get; }

/// <summary>
/// All unit names for the quantity, such as ["Centimeter", "Decimeter", "Meter", ...].
/// </summary>
[Obsolete("This property is deprecated and will be removed at a future release. Please use the UnitInfos property.")]
public string[] UnitNames { get; }

/// <summary>
/// All unit enum values for the quantity, such as [<see cref="LengthUnit.Centimeter" />,
/// <see cref="LengthUnit.Decimeter" />, <see cref="LengthUnit.Meter" />, ...].
/// </summary>
[Obsolete("This property is deprecated and will be removed at a future release. Please use the UnitInfos property.")]
public Enum[] Units { get; }

public UnitInfo BaseUnitInfo { get; }

/// <summary>
/// The base unit for the quantity, such as <see cref="LengthUnit.Meter" />.
/// </summary>
[Obsolete("This property is deprecated and will be removed at a future release. Please use the BaseUnitInfo property.")]
public Enum BaseUnit { get; }

/// <summary>
Expand Down Expand Up @@ -134,13 +143,24 @@ public QuantityInfo(QuantityType quantityType, TUnit[] units, TUnit baseUnit, IQ
: base(quantityType, units.Cast<Enum>().ToArray(), baseUnit, zero, baseDimensions)
{
Zero = zero;
UnitInfos = units.Select(unit => new UnitInfo<TUnit>(unit)).ToArray();
Units = units;
BaseUnit = baseUnit;
BaseUnitInfo = new UnitInfo<TUnit>(baseUnit);
BaseUnit = BaseUnitInfo.Value;
}

/// <inheritdoc cref="QuantityInfo.UnitInfos" />
public new UnitInfo<TUnit>[] UnitInfos { get; }

/// <inheritdoc cref="QuantityInfo.Units" />
[Obsolete("This property is deprecated and will be removed at a future release. Please use the UnitInfos property.")]
public new TUnit[] Units { get; }

/// <inheritdoc cref="QuantityInfo.BaseUnitInfo" />
public new UnitInfo<TUnit> BaseUnitInfo { get; }

/// <inheritdoc cref="QuantityInfo.BaseUnit" />
[Obsolete("This property is deprecated and will be removed at a future release. Please use the BaseUnitInfo property.")]
public new TUnit BaseUnit { get; }

/// <inheritdoc cref="QuantityInfo.Zero" />
Expand Down
80 changes: 80 additions & 0 deletions UnitsNet/UnitInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com).
// https://github.com/angularsen/UnitsNet
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using UnitsNet.Units;

namespace UnitsNet
{
/// <summary>
/// Information about the unit, such as its name and value.
/// This is useful to enumerate units and present names with quantities and units
/// chosen dynamically at runtime, such as unit conversion apps or allowing the user to change the
/// unit representation.
/// </summary>
/// <remarks>
/// Typically you obtain this by looking it up via <see cref="QuantityInfo.UnitInfos" />.
/// </remarks>
#if WINDOWS_UWP
internal
#else
public
#endif
class UnitInfo
{
public UnitInfo(Enum value)
{
Value = value;
Name = value.ToString();
}

/// <summary>
/// The enum value of the unit, such as [<see cref="LengthUnit.Centimeter" />,
/// <see cref="LengthUnit.Decimeter" />, <see cref="LengthUnit.Meter" />, ...].
/// </summary>
public Enum Value;

/// <summary>
/// The name of the unit, such as ["Centimeter", "Decimeter", "Meter", ...].
/// </summary>
public string Name { get; }
}

#if !WINDOWS_UWP
/// <inheritdoc cref="UnitInfo" />
/// <remarks>
/// This is a specialization of <see cref="UnitInfo" />, for when the unit type is known.
/// Typically you obtain this by looking it up statically from <see cref="QuantityInfo{LengthUnit}.UnitInfos" /> or
/// or dynamically via <see cref="IQuantity{TUnitType}.QuantityInfo" />.
/// </remarks>
/// <typeparam name="TUnit">The unit enum type, such as <see cref="LengthUnit" />. </typeparam>
public class UnitInfo<TUnit> : UnitInfo
where TUnit : Enum
{
public UnitInfo(TUnit value) : base(value)
{
Value = value;
}

public new TUnit Value { get; }
}
#endif
}