-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a440f9
commit c81e1bb
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
|
||
namespace UnitsNet.Tests.CustomQuantities | ||
{ | ||
/// <inheritdoc cref="IQuantity"/> | ||
/// <summary> | ||
/// Example of a custom/third-party quantity implementation, for plugging in quantities and units at runtime. | ||
/// </summary> | ||
public struct HowMuch : IQuantity | ||
{ | ||
public HowMuch(double value, Enum unit) : this() | ||
{ | ||
Unit = unit; | ||
Value = value; | ||
} | ||
|
||
public Enum Unit { get; } | ||
public double Value { get; } | ||
|
||
#region Crud to satisfy IQuantity, but not really used for anything | ||
|
||
private static readonly HowMuch Zero = new HowMuch(0, HowMuchUnit.Some); | ||
|
||
public QuantityType Type => QuantityType.Undefined; | ||
public BaseDimensions Dimensions => BaseDimensions.Dimensionless; | ||
|
||
public QuantityInfo QuantityInfo => new QuantityInfo(Type, | ||
new UnitInfo[] | ||
{ | ||
new UnitInfo<HowMuchUnit>(HowMuchUnit.Some, BaseUnits.Undefined), | ||
new UnitInfo<HowMuchUnit>(HowMuchUnit.ATon, BaseUnits.Undefined), | ||
new UnitInfo<HowMuchUnit>(HowMuchUnit.AShitTon, BaseUnits.Undefined), | ||
}, | ||
HowMuchUnit.Some, | ||
Zero, | ||
BaseDimensions.Dimensionless); | ||
|
||
public double As(Enum unit) => Convert.ToDouble(unit); | ||
|
||
public double As(UnitSystem unitSystem) => throw new NotImplementedException(); | ||
|
||
public IQuantity ToUnit(Enum unit) => new HowMuch(As(unit), unit); | ||
|
||
public IQuantity ToUnit(UnitSystem unitSystem) => throw new NotImplementedException(); | ||
|
||
public string ToString(string format, IFormatProvider formatProvider) => $"HowMuch ({format}, {formatProvider})"; | ||
public string ToString(IFormatProvider provider) => $"HowMuch ({provider})"; | ||
public string ToString(IFormatProvider provider, int significantDigitsAfterRadix) => $"HowMuch ({provider}, {significantDigitsAfterRadix})"; | ||
public string ToString(IFormatProvider provider, string format, params object[] args) => $"HowMuch ({provider}, {string.Join(", ", args)})"; | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace UnitsNet.Tests.CustomQuantities | ||
{ | ||
/// <summary> | ||
/// Example of a custom/third-party quantity implementation, for plugging in quantities and units at runtime. | ||
/// </summary> | ||
public enum HowMuchUnit | ||
{ | ||
Some, | ||
ATon, | ||
AShitTon | ||
} | ||
} |