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
💥Require unit enum types to also be struct (#1472)
Change all `where TUnitType : Enum` generic type constraints to include
`struct` constraint, to avoid usages like this.
```cs
UnitsNetSetup.Default.UnitParser.Parse<LengthUnit>("abc"); // Ok
UnitsNetSetup.Default.UnitParser.Parse<Enum>("abc"); // Compiles, but throws exception
```
This also fixes inconsistency, between
`UnitAbbreviationsCache.GetUnitAbbreviations<TUnitType>` and
`UnitAbbreviationsCache.GetDefaultAbbreviation<TUnitType>`.
### Changes
- Add `struct` constraint to all `Enum` constraints
- Remove two test cases
`MapAndLookup_MapWithSpecificEnumType_LookupWithEnumType`,
`MapAndLookup_WithEnumType`
### Noteworthy
There are some minor use cases for passing non-generic `Enum` value,
like getting unit abbreviations for an `IQuantity` such as
`UnitAbbreviations.GetDefaultAbbreviation(quantity.Unit)`.
After this PR, you now have to do
`GetDefaultAbbreviation(quantity.Unit.GetType(),
Convert.ToInt32(quantity.Unit))` instead.
`GetAbbreviations()` already had this requirement, so now it's more
consistent at least.
### Sources
https://stackoverflow.com/a/74773273https://devblogs.microsoft.com/premier-developer/dissecting-new-generics-constraints-in-c-7-3/
Copy file name to clipboardExpand all lines: UnitsNet/CustomCode/QuantityParser.cs
+6-6
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ namespace UnitsNet
19
19
/// <typeparam name="TUnitType">The type of unit enum that belongs to this quantity, such as <see cref="LengthUnit"/> for <see cref="Length"/>.</typeparam>
0 commit comments