-
Notifications
You must be signed in to change notification settings - Fork 404
More complete sample of custom units #1189
-
I'm looking for a more complete sample of a custom units. I've been looking at this, and I have a class that works fine in isolation, but is I don't see how it integrates with the rest of the system, like using UnitConverter or abbreviations.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 4 replies
-
Custom units is the least mature part of the library. It is mostly exploratory to see what we can do with it, in particular for those that want to add units that we don't accept in the main library.
I see the wiki doesn't really explain why one would add a custom unit, and to be honest, the benefits are small in its current state.
You lose out on what I think is the biggest benefit of the library, such as generating quantity types with factory method FromMeters() and conversion properties Meters. C# source generators might provide something like that.
Key benefits
As I recall at least.
- Reuse functionality that operates on
IQuantity- Dynamically convert to unit with
.As(Enum) UnitMathwith min, max, sum, average etc.- .NET generic math, currently Sum() and Average()
- Dynamically convert to unit with
- Reuse
UnitConverterto dynamically convert between units by their enum values- Also allows you to dynamically convert between your custom units and the built-in units, such as
CustomLengthUnit.ElbowToThumbtoLengthUnit.Meter.
- Also allows you to dynamically convert between your custom units and the built-in units, such as
- Reuse
QuantityParserandUnitParserto parse quantity strings like "5 cm" and "cm" for your own quantities and units
There are probably more I'm forgetting.
What could be better
- Source generators via nuget, if possible Using source generators #902
- String-based lookup instead of enum-based, blocks XP One nuget per quantity #1181
I'm adding this to the wiki.
Did that answer your question?
It would also be helpful to know;
What did you expect the integration to do?
Anything we can add to make it better?
Beta Was this translation helpful? Give feedback.
All reactions
-
I need to sum up all quantities for an order. Most of the quantities are expressed in weight, but some are expressed in "case" or "each". I didn't see a way to add a unit to MassUnit, so I created a new unit OrderQuantityUnit and a new quantity implementation OrderQuantity.
If I now use UnitMath.Sum(), I get the error:
Unit value Pound of type SharedKernel.ValueObjects.OrderQuantityUnit is not a known unit enum type. Expected types like UnitsNet.Units.LengthUnit. Did you pass in a third-party enum type defined outside UnitsNet library?
It makes sense, but I also don't see how to make it work.
Beta Was this translation helpful? Give feedback.
All reactions
-
You can check the implementation of UnitMath.Sum():
https://github.com/angularsen/UnitsNet/blob/master/UnitsNet/UnitMath.cs#L33-L37
public static TQuantity Sum<TQuantity>(this IEnumerable<TQuantity> source, Enum unitType)
where TQuantity : IQuantity
{
return (TQuantity) Quantity.From(source.Sum(x => x.As(unitType)), unitType);
}
As you can see, it basically calls As(unitType) on each IQuantity you pass in.
It then calls Quantity.From(double, Enum) to construct the quantity.
This is where it fails, this part is still hard-coded to just the built-in quantities.
https://github.com/angularsen/UnitsNet/blob/master/UnitsNet/GeneratedCode/Quantity.g.cs#L297
We could make this extensible, so you can plug in your own quantities and then Sum() should work.
As mentioned earlier, custom units is not very mature yet.
If you are interested in attempting a pull request, I'm happy to assist and provide pointers on how it could be extended.
I don't have much spare time to spend on this project myself these days.
Beta Was this translation helpful? Give feedback.
All reactions
-
This is 1,5 years old. Was there any progress?
I need to calculate different H profiles and tubes to kg for example.
Beta Was this translation helpful? Give feedback.
All reactions
-
Not much new since then I think, v6 is working on simplifying the IQuantity interface and relying more on extension methods, other than that I believe the above summary still applies.
Beta Was this translation helpful? Give feedback.
All reactions
-
You all might want to check these Samples:
https://github.com/lipchev/UnitsNet/tree/fractional-quantity-value/Samples/UnitsNetSetup.Configuration
Beta Was this translation helpful? Give feedback.