Skip to content

Commit c81e1bb

Browse files
committed
Add HowMuch custom quantity
1 parent 5a440f9 commit c81e1bb

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
3+
namespace UnitsNet.Tests.CustomQuantities
4+
{
5+
/// <inheritdoc cref="IQuantity"/>
6+
/// <summary>
7+
/// Example of a custom/third-party quantity implementation, for plugging in quantities and units at runtime.
8+
/// </summary>
9+
public struct HowMuch : IQuantity
10+
{
11+
public HowMuch(double value, Enum unit) : this()
12+
{
13+
Unit = unit;
14+
Value = value;
15+
}
16+
17+
public Enum Unit { get; }
18+
public double Value { get; }
19+
20+
#region Crud to satisfy IQuantity, but not really used for anything
21+
22+
private static readonly HowMuch Zero = new HowMuch(0, HowMuchUnit.Some);
23+
24+
public QuantityType Type => QuantityType.Undefined;
25+
public BaseDimensions Dimensions => BaseDimensions.Dimensionless;
26+
27+
public QuantityInfo QuantityInfo => new QuantityInfo(Type,
28+
new UnitInfo[]
29+
{
30+
new UnitInfo<HowMuchUnit>(HowMuchUnit.Some, BaseUnits.Undefined),
31+
new UnitInfo<HowMuchUnit>(HowMuchUnit.ATon, BaseUnits.Undefined),
32+
new UnitInfo<HowMuchUnit>(HowMuchUnit.AShitTon, BaseUnits.Undefined),
33+
},
34+
HowMuchUnit.Some,
35+
Zero,
36+
BaseDimensions.Dimensionless);
37+
38+
public double As(Enum unit) => Convert.ToDouble(unit);
39+
40+
public double As(UnitSystem unitSystem) => throw new NotImplementedException();
41+
42+
public IQuantity ToUnit(Enum unit) => new HowMuch(As(unit), unit);
43+
44+
public IQuantity ToUnit(UnitSystem unitSystem) => throw new NotImplementedException();
45+
46+
public string ToString(string format, IFormatProvider formatProvider) => $"HowMuch ({format}, {formatProvider})";
47+
public string ToString(IFormatProvider provider) => $"HowMuch ({provider})";
48+
public string ToString(IFormatProvider provider, int significantDigitsAfterRadix) => $"HowMuch ({provider}, {significantDigitsAfterRadix})";
49+
public string ToString(IFormatProvider provider, string format, params object[] args) => $"HowMuch ({provider}, {string.Join(", ", args)})";
50+
51+
#endregion
52+
}
53+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace UnitsNet.Tests.CustomQuantities
2+
{
3+
/// <summary>
4+
/// Example of a custom/third-party quantity implementation, for plugging in quantities and units at runtime.
5+
/// </summary>
6+
public enum HowMuchUnit
7+
{
8+
Some,
9+
ATon,
10+
AShitTon
11+
}
12+
}

0 commit comments

Comments
 (0)