Skip to content

Commit

Permalink
♻️Allow custom units to set UnitInfo.QuantityName (#1421)
Browse files Browse the repository at this point in the history
Related #1418
Port of #1420 

- Make `UnitInfo` ctor public, to accept quantity name for custom units
- Add null checks to ctors, and require quantity name in ctor that takes
one
- Mark ctor without quantity name obsolete
  • Loading branch information
angularsen authored Aug 31, 2024
1 parent ebe0b32 commit 7543402
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions UnitsNet/UnitInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public class UnitInfo
/// <param name="value">The enum value for this class, for example <see cref="LengthUnit.Meter"/>.</param>
/// <param name="pluralName">The plural name of the unit, such as "Centimeters".</param>
/// <param name="baseUnits">The <see cref="BaseUnits"/> for this unit.</param>
[Obsolete("Use the constructor that also takes a quantityName parameter.")]
public UnitInfo(Enum value, string pluralName, BaseUnits baseUnits)
{
Value = value ?? throw new ArgumentNullException(nameof(value));
Name = value.ToString();
PluralName = pluralName;
PluralName = pluralName ?? throw new ArgumentNullException(nameof(pluralName));
BaseUnits = baseUnits ?? throw new ArgumentNullException(nameof(baseUnits));
}

Expand All @@ -38,10 +39,13 @@ public UnitInfo(Enum value, string pluralName, BaseUnits baseUnits)
/// <param name="pluralName">The plural name of the unit, such as "Centimeters".</param>
/// <param name="baseUnits">The <see cref="BaseUnits"/> for this unit.</param>
/// <param name="quantityName">The quantity name that this unit is for.</param>
internal UnitInfo(Enum value, string pluralName, BaseUnits baseUnits, string quantityName) :
this(value, pluralName, baseUnits)
public UnitInfo(Enum value, string pluralName, BaseUnits baseUnits, string quantityName)
{
QuantityName = quantityName;
Value = value ?? throw new ArgumentNullException(nameof(value));
Name = value.ToString();
PluralName = pluralName ?? throw new ArgumentNullException(nameof(pluralName));
BaseUnits = baseUnits ?? throw new ArgumentNullException(nameof(baseUnits));
QuantityName = quantityName ?? throw new ArgumentNullException(nameof(quantityName));
}

/// <summary>
Expand Down Expand Up @@ -81,14 +85,15 @@ public class UnitInfo<TUnit> : UnitInfo
where TUnit : Enum
{
/// <inheritdoc />
[Obsolete("Use the constructor that also takes a quantityName parameter.")]
public UnitInfo(TUnit value, string pluralName, BaseUnits baseUnits) :
base(value, pluralName, baseUnits)
{
Value = value;
}

/// <inheritdoc />
internal UnitInfo(TUnit value, string pluralName, BaseUnits baseUnits, string quantityName) :
public UnitInfo(TUnit value, string pluralName, BaseUnits baseUnits, string quantityName) :
base(value, pluralName, baseUnits, quantityName)
{
Value = value;
Expand Down

0 comments on commit 7543402

Please sign in to comment.