Skip to content

Commit 20062c7

Browse files
authored
♻️Allow custom units to set UnitInfo.QuantityName (#1420)
Fixes #1418 - 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
1 parent 0eff43e commit 20062c7

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

UnitsNet/UnitInfo.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ public class UnitInfo
2323
/// <param name="value">The enum value for this class, for example <see cref="LengthUnit.Meter"/>.</param>
2424
/// <param name="pluralName">The plural name of the unit, such as "Centimeters".</param>
2525
/// <param name="baseUnits">The <see cref="BaseUnits"/> for this unit.</param>
26+
[Obsolete("Use the constructor that also takes a quantityName parameter.")]
2627
public UnitInfo(Enum value, string pluralName, BaseUnits baseUnits)
2728
{
2829
Value = value ?? throw new ArgumentNullException(nameof(value));
2930
Name = value.ToString();
30-
PluralName = pluralName;
31+
PluralName = pluralName ?? throw new ArgumentNullException(nameof(pluralName));
3132
BaseUnits = baseUnits ?? throw new ArgumentNullException(nameof(baseUnits));
3233
}
3334

@@ -38,10 +39,13 @@ public UnitInfo(Enum value, string pluralName, BaseUnits baseUnits)
3839
/// <param name="pluralName">The plural name of the unit, such as "Centimeters".</param>
3940
/// <param name="baseUnits">The <see cref="BaseUnits"/> for this unit.</param>
4041
/// <param name="quantityName">The quantity name that this unit is for.</param>
41-
internal UnitInfo(Enum value, string pluralName, BaseUnits baseUnits, string quantityName) :
42-
this(value, pluralName, baseUnits)
42+
public UnitInfo(Enum value, string pluralName, BaseUnits baseUnits, string quantityName)
4343
{
44-
QuantityName = quantityName;
44+
Value = value ?? throw new ArgumentNullException(nameof(value));
45+
Name = value.ToString();
46+
PluralName = pluralName ?? throw new ArgumentNullException(nameof(pluralName));
47+
BaseUnits = baseUnits ?? throw new ArgumentNullException(nameof(baseUnits));
48+
QuantityName = quantityName ?? throw new ArgumentNullException(nameof(quantityName));
4549
}
4650

4751
/// <summary>
@@ -81,14 +85,15 @@ public class UnitInfo<TUnit> : UnitInfo
8185
where TUnit : Enum
8286
{
8387
/// <inheritdoc />
88+
[Obsolete("Use the constructor that also takes a quantityName parameter.")]
8489
public UnitInfo(TUnit value, string pluralName, BaseUnits baseUnits) :
8590
base(value, pluralName, baseUnits)
8691
{
8792
Value = value;
8893
}
8994

9095
/// <inheritdoc />
91-
internal UnitInfo(TUnit value, string pluralName, BaseUnits baseUnits, string quantityName) :
96+
public UnitInfo(TUnit value, string pluralName, BaseUnits baseUnits, string quantityName) :
9297
base(value, pluralName, baseUnits, quantityName)
9398
{
9499
Value = value;

0 commit comments

Comments
 (0)