Skip to content

Commit 7c86c39

Browse files
committed
Update code to account for UnitKey changes
1 parent 4f7ad28 commit 7c86c39

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

UnitsNet/CustomCode/QuantityParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ private static bool TryExtractValueAndUnit(Regex regex, string str, [NotNullWhen
246246

247247
private string CreateRegexPatternForQuantity<TUnitType>(IFormatProvider? formatProvider) where TUnitType : struct, Enum
248248
{
249-
var unitAbbreviations = _unitAbbreviationsCache.GetAllUnitAbbreviationsForQuantity<TUnitType>(formatProvider);
249+
var unitAbbreviations = _unitAbbreviationsCache.GetAllUnitAbbreviationsForQuantity(typeof(TUnitType), formatProvider);
250250
var pattern = GetRegexPatternForUnitAbbreviations(unitAbbreviations);
251251

252252
// Match entire string exactly

UnitsNet/CustomCode/UnitAbbreviationsCache.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Resources;
1010
using UnitsNet.InternalHelpers;
11+
using System.Diagnostics.CodeAnalysis;
1112
using UnitsNet.Units;
1213
using AbbreviationMapKey = System.ValueTuple<UnitsNet.UnitKey, string>;
1314

@@ -275,16 +276,19 @@ private bool TryGetUnitAbbreviations(UnitKey unitKey, IFormatProvider? formatPro
275276
/// <param name="unitEnumType">Enum type for unit.</param>
276277
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
277278
/// <returns>Unit abbreviations associated with unit.</returns>
278-
[RequiresDynamicCode("It might not be possible to create an array of the enum type at runtime. Use the GetAllUnitAbbreviationsForQuantity<TEnum> overload.")]
279+
/// <exception cref="QuantityNotFoundException">
280+
/// Thrown when no quantity information is found for the specified unit enum type.
281+
/// </exception>
279282
public IReadOnlyList<string> GetAllUnitAbbreviationsForQuantity(Type unitEnumType, IFormatProvider? formatProvider = null)
280283
{
281284
var allAbbreviations = new List<string>();
282285
if (!QuantityInfoLookup.TryGetQuantityByUnitType(unitEnumType, out QuantityInfo? quantityInfo))
283286
{
284287
// TODO I think we should either return empty or throw QuantityNotFoundException here
285-
var enumValues = Enum.GetValues(unitEnumType).Cast<Enum>();
286-
var all = GetStringUnitPairs(enumValues, formatProvider);
287-
return all.Select(pair => pair.Item2).ToList();
288+
// var enumValues = Enum.GetValues(unitEnumType).Cast<Enum>();
289+
// var all = GetStringUnitPairs(enumValues, formatProvider);
290+
// return all.Select(pair => pair.Item2).ToList();
291+
throw new QuantityNotFoundException("No quantity information was found for the type.");
288292
}
289293

290294
foreach(UnitInfo unitInfo in quantityInfo.UnitInfos)

UnitsNet/UnitConverter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ public static double ConvertByAbbreviation(double fromValue, string quantityName
453453
/// </exception>
454454
/// <exception cref="UnitNotFoundException">No units match the abbreviation.</exception>
455455
/// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbreviation.</exception>
456+
[RequiresDynamicCode("It might not be possible to convert by abbreviation at runtime")]
456457
public static double ConvertByAbbreviation(double fromValue, string quantityName, string fromUnitAbbrev, string toUnitAbbrev, IFormatProvider? formatProvider)
457458
{
458459
QuantityInfoLookup quantities = UnitsNetSetup.Default.QuantityInfoLookup;
@@ -536,6 +537,7 @@ public static bool TryConvertByAbbreviation(double fromValue, string quantityNam
536537
/// <param name="result">Result if conversion was successful, 0 if not.</param>
537538
/// <example>double centimeters = ConvertByName(5, "Length", "m", "cm"); // 500</example>
538539
/// <returns>True if conversion was successful.</returns>
540+
[RequiresDynamicCode("It might not be possible to convert by abbreviation at runtime")]
539541
public static bool TryConvertByAbbreviation(double fromValue, string quantityName, string fromUnitAbbrev, string toUnitAbbrev, out double result,
540542
IFormatProvider? formatProvider)
541543
{

0 commit comments

Comments
 (0)