Commit 2a60bf9
Add IQuantity.UnitInfo convenience property (#1649)
## Summary
- Adds `IQuantity.UnitInfo` default interface member on .NET 5+ targets,
so consumers can write `quantity.UnitInfo` instead of
`quantity.QuantityInfo[quantity.UnitKey]`
- Adds typed `IQuantity<TUnitType>.UnitInfo` returning
`UnitInfo<TUnitType>` on .NET 5+
- Adds `GetUnitInfo()` and `GetUnitInfo<TUnit>()` extension methods on
`netstandard2.0` for the same functionality
## Motivation
Getting `UnitInfo` from a quantity instance currently requires the
verbose `quantity.QuantityInfo[quantity.UnitKey]`. This is a common
operation, especially when chaining with APIs like
`UnitAbbreviationsCache` that benefit from having `UnitInfo` directly
(see #1067).
## Design decisions
### TFM fragmentation
The extension methods are gated behind `#if !NET` so that .NET 5+
consumers only see the cleaner property syntax in IntelliSense. Library
authors who multi-target can use
`quantity.QuantityInfo[quantity.UnitKey]` which works on all targets, or
write a one-line wrapper. If this turns out to be a real pain point,
removing the `#if` guard is a non-breaking one-line change.
### Extension method tests
The test project targets `net8.0;net9.0;net10.0` only, so `#if !NET`
code is never compiled in tests. The extension methods are trivial
one-liner wrappers over the same indexer call that the default interface
members use, so they are effectively covered by the property tests.
## Test plan
- [x] `dotnet build UnitsNet/UnitsNet.csproj` — 0 errors across all 4
target frameworks
- [x] New tests pass on net8.0, net9.0, net10.0:
- `UnitInfo_ReturnsUnitInfoForQuantityUnit` — non-base unit via
`IQuantity`
- `UnitInfo_Zero_ReturnsBaseUnitInfo` — `.Zero` instance
- `UnitInfo_TypedQuantity_ReturnsTypedUnitInfo` — typed
`IQuantity<LengthUnit>`
- `UnitInfo_MatchesUnit` — `Assert.All` across all quantities
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 414fe57 commit 2a60bf9
File tree
3 files changed
+90
-0
lines changed- UnitsNet.Tests/CustomCode
- UnitsNet
- Extensions
3 files changed
+90
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
62 | 104 | | |
63 | 105 | | |
64 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
14 | 46 | | |
15 | 47 | | |
16 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
61 | 72 | | |
62 | 73 | | |
63 | 74 | | |
| |||
94 | 105 | | |
95 | 106 | | |
96 | 107 | | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
97 | 111 | | |
98 | 112 | | |
| 113 | + | |
| 114 | + | |
99 | 115 | | |
100 | 116 | | |
101 | 117 | | |
| |||
0 commit comments