|
| 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 | +} |
0 commit comments