🐛 Fix unit abbreviation lookup for unit value cast to Enum - #1302
Merged
Conversation
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 ReportPatch coverage:
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
☔ View full report in Codecov by Sentry. |
angularsen
enabled auto-merge (squash)
August 10, 2023 19:17
angularsen
disabled auto-merge
August 10, 2023 19:20
angularsen
enabled auto-merge (squash)
August 10, 2023 19:20
Owner
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 usingtypeof(TUnitEnum)in the generic method.Enumsatisfies the generic constraint, but the generic type no longer describes the original unit enum type. Instead, we must useunitEnumValue.GetType().Changes
Enumtype is passed instead of an actual unit enum type