Skip to content

🐛 Fix unit abbreviation lookup for unit value cast to Enum - #1302

Merged
angularsen merged 4 commits into
masterfrom
agl/fix-getabbrev-enum
Aug 10, 2023
Merged

🐛 Fix unit abbreviation lookup for unit value cast to Enum#1302
angularsen merged 4 commits into
masterfrom
agl/fix-getabbrev-enum

Conversation

@angularsen

@angularsen angularsen commented Aug 10, 2023

Copy link
Copy Markdown
Owner

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

Fixes #1301

When calling methods to look up unit abbreviation for a unit enum value,
which is cast to Enum either by variable or method parameter,
then the actual enum type was lost and it failed to get its enum name.

```
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
@codecov

codecov Bot commented Aug 10, 2023

Copy link
Copy Markdown

Codecov Report

Patch coverage: 100% and no project coverage change.

Comparison is base (e2720b0) 84% compared to head (6f5a278) 84%.

Additional details and impacted files
@@          Coverage Diff           @@
##           master   #1302   +/-   ##
======================================
  Coverage      84%     84%           
======================================
  Files         336     336           
  Lines       31917   31918    +1     
======================================
+ Hits        26902   26903    +1     
  Misses       5015    5015           
Files Changed Coverage Δ
UnitsNet/CustomCode/UnitAbbreviationsCache.cs 88% <100%> (+<1%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@angularsen
angularsen enabled auto-merge (squash) August 10, 2023 19:17
@angularsen
angularsen disabled auto-merge August 10, 2023 19:20
@angularsen
angularsen enabled auto-merge (squash) August 10, 2023 19:20
@angularsen
angularsen merged commit 73f53bd into master Aug 10, 2023
@angularsen
angularsen deleted the agl/fix-getabbrev-enum branch August 10, 2023 19:36
@angularsen

Copy link
Copy Markdown
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Problem with ENUMS when UnitAbbreviationsCache.Default.GetDefaultAbbreviation is use.

1 participant