Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Aug 10, 2023

Conversation

angularsen
Copy link
Owner

@angularsen angularsen commented Aug 10, 2023

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
Copy link

codecov bot commented Aug 10, 2023

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 enabled auto-merge (squash) August 10, 2023 19:20
@angularsen angularsen merged commit 73f53bd into master Aug 10, 2023
3 checks passed
@angularsen angularsen deleted the agl/fix-getabbrev-enum branch August 10, 2023 19:36
@angularsen
Copy link
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