You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Docs/extending-with-custom-units.md
+29-13Lines changed: 29 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,31 @@
1
1
# Extending with Custom Units
2
2
3
+
## Recommended prototype: UnitsNet.Modular
4
+
5
+
[UnitsNet.Modular](../UnitsNet.Modular/README.md) is our prototype for a better way to add
6
+
application-specific quantities and units. It is a proof of concept and currently in pre-release,
7
+
but it generates strongly typed quantity structs, unit enums, conversions, parsing, formatting, and
8
+
metadata from your JSON definitions at compile time.
9
+
10
+
Start with [Add custom quantities](../UnitsNet.Modular/README.md#add-custom-quantities), or explore the
11
+
[UnitsNet.Modular samples in GitHub Codespaces](https://codespaces.new/angularsen/UnitsNet?devcontainer_path=.devcontainer%2Funitsnet-modular%2Fdevcontainer.json&quickstart=1).
12
+
13
+
`UnitsNet` and `UnitsNet.Modular` are alternative implementations and cannot be referenced together
14
+
in the same consumer project. If you need to keep using the established `UnitsNet` package, the
15
+
runtime approach below remains available.
16
+
17
+
## Secondary approach: runtime custom quantities in UnitsNet
18
+
3
19
This article is for when you want to add your own custom quantities and units at runtime, not included in the UnitsNet nuget.
4
20
5
21
To add new quantities or units to the `UnitsNet` nuget, please see [Adding a New Unit](adding-a-new-unit.md).
6
22
7
-
## Disclaimer: This is highly experimental and incomplete
23
+
###Disclaimer: This is highly experimental and incomplete
8
24
9
25
You miss out on the statically generated code for members like `Length.FromMeters(1)` and `myLength.Meters`.
10
26
Conversion methods like `myLength.As()` and `myLength.ToUnit()` currently only support their respective unit enums, in this case `LengthUnit`.
11
27
12
-
## Can I add a custom unit to an existing quantity in UnitsNet?
28
+
###Can I add a custom unit to an existing quantity in UnitsNet?
13
29
14
30
Currently, no.
15
31
@@ -21,14 +37,14 @@ Since UnitsNet is so statically typed, your options are limited to:
21
37
1. Submit a pull request to [add a new unit](adding-a-new-unit.md) to the UnitsNet nuget
22
38
2. Build your own custom version of UnitsNet
23
39
24
-
## Why add a custom quantity?
40
+
###Why add a custom quantity?
25
41
26
42
Good question.
27
43
28
44
In its current state, the support for custom quantities and units is limited and provides limited integration with the existing units and code.
29
45
We consider it exploratory, to see what is possible, and we welcome ideas on how it can be improved.
30
46
31
-
### Key benefits
47
+
####Key benefits
32
48
33
49
- Reuse functionality that operates on `IQuantity`
34
50
- Dynamically convert to unit with `.As(Enum)`
@@ -38,14 +54,14 @@ We consider it exploratory, to see what is possible, and we welcome ideas on how
38
54
- Also allows you to dynamically convert between your custom units and the built-in units, such as `CustomLengthUnit.ElbowToThumb` to `LengthUnit.Meter`.
39
55
- Reuse `QuantityParser` and `UnitParser` to parse quantity strings like "5 cm" and "cm" for your own quantities and units
40
56
41
-
### What could be better
57
+
####What could be better
42
58
43
59
- Source generators via nuget, if possible [Using source generators #902](https://github.com/angularsen/UnitsNet/issues/902)
44
60
- String-based lookup instead of enum-based for quantity methods like `As()` and `ToUnit()`, required for [XP One nuget per quantity #1181](https://github.com/angularsen/UnitsNet/pull/1181)
45
61
46
62
Got more ideas? Create a discussion or issue.
47
63
48
-
## Units.NET structure
64
+
###Units.NET structure
49
65
50
66
Units.NET roughly consists of these parts:
51
67
* Quantities like `Length` and `Force`
@@ -54,9 +70,9 @@ Units.NET roughly consists of these parts:
54
70
*[JSON files for defining units, conversion functions and abbreviations](quantity-and-unit-definition-schema.md)
55
71
*`CodeGen` console app to generate C# code based on JSON files
56
72
57
-
## Example: Custom quantity `HowMuch` with units `HowMuchUnit`
73
+
###Example: Custom quantity `HowMuch` with units `HowMuchUnit`
Copy file name to clipboardExpand all lines: UnitsNet.Modular/README.md
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,8 @@
1
1
# UnitsNet Modular
2
2
3
+
[](https://codespaces.new/angularsen/UnitsNet?devcontainer_path=.devcontainer%2Funitsnet-modular%2Fdevcontainer.json&quickstart=1)
[](https://codespaces.new/angularsen/UnitsNet?devcontainer_path=.devcontainer%2Funitsnet-modular%2Fdevcontainer.json&quickstart=1)
5
6
6
7
Generate only the strongly typed quantities and units your application needs.
7
8
@@ -23,13 +24,13 @@ the complete catalog:
23
24
- immutable runtime discovery and System.Text.Json support;
24
25
- trimming and Native AOT-friendly generated code.
25
26
26
-
> **Experimental:** UnitsNet.Modular is an alpha proof of concept. Its API, package structure, and
27
-
> compatibility guarantees may change as the architecture is evaluated.
27
+
> **Experimental:** UnitsNet.Modular is a proof of concept and currently in pre-release. Its API,
28
+
> package structure, and compatibility guarantees may change as the architecture is evaluated.
28
29
29
30
Want to try it without installing anything? Open the browser-based
Copy file name to clipboardExpand all lines: UnitsNet/UnitsNet.csproj
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@
5
5
<Version>6.0.0-pre021</Version>
6
6
<Authors>Andreas Gullberg Larsen</Authors>
7
7
<Title>Units.NET</Title>
8
-
<Description>Get all the common units of measurement and the conversions between them. It is light-weight and thoroughly tested.</Description>
8
+
<Description>Add strongly typed quantities and units to your code and get merrily on with your life. No more magic constants found online or guessing the unit of variables.</Description>
9
9
<Copyright>Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com).</Copyright>
0 commit comments