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
🐛 Fix unit abbreviation lookup for unit value cast to Enum (#1302)
Fixes#1301
When looking up unit abbreviation for a unit enum value that is cast to `Enum`, for example via variable or method parameter, the lookup failed due to using `typeof(TUnitEnum)` in the generic method. `Enum` satisfies the generic constraint, but the generic type no longer describes the original unit enum type. Instead, we must use `unitEnumValue.GetType()`.
```
System.ArgumentException: Type provided must be an Enum.
at System.Enum.GetEnumInfo(RuntimeType enumType, Boolean getNames)
at System.RuntimeType.GetEnumName(Object value)
at UnitsNet.UnitAbbreviationsCache.TryGetUnitAbbreviations(Type unitType, Int32 unitValue, IFormatProvider formatProvider, String[]& abbreviations) in C:\dev\unitsnet\UnitsNet\CustomCode\UnitAbbreviationsCache.cs:line 246
```
### Changes
- Handle edge-case when `Enum` type is passed instead of an actual unit enum type
/// Test that lookup works when specifying unit enum value both as cast to <see cref="Enum"/> and as specific enum value type.
307
+
/// We have had subtle bugs when <see cref="Enum"/> is passed to generic methods with constraint TUnitEnum : Enum,
308
+
/// which the Enum type satisfies, but trying to use typeof(TUnitEnum) no longer represent the actual enum type so unitEnumValue.GetType() should be used.
0 commit comments