@@ -40,10 +40,18 @@ namespace UnitsNet
40
40
/// </summary>
41
41
public static class UnitConverter
42
42
{
43
- private static readonly string QuantityNamespace = typeof ( Length ) . Namespace ;
44
43
private static readonly string UnitTypeNamespace = typeof ( LengthUnit ) . Namespace ;
45
44
private static readonly Assembly UnitsNetAssembly = typeof ( Length ) . GetAssembly ( ) ;
46
45
46
+ private static readonly Type [ ] QuantityTypes = UnitsNetAssembly . GetTypes ( )
47
+ . Where ( typeof ( IQuantity ) . IsAssignableFrom )
48
+ . Where ( x => x . IsClass ( ) || x . IsValueType ( ) ) // Future-proofing: we are discussing changing quantities from struct to class
49
+ . ToArray ( ) ;
50
+
51
+ private static readonly Type [ ] UnitTypes = UnitsNetAssembly . GetTypes ( )
52
+ . Where ( x => x . Namespace == UnitTypeNamespace && x . IsEnum ( ) && x . Name . EndsWith ( "Unit" ) )
53
+ . ToArray ( ) ;
54
+
47
55
/// <summary>
48
56
/// Convert between any two quantity units by their names, such as converting a "Length" of N "Meter" to "Centimeter".
49
57
/// This is particularly useful for creating things like a generated unit conversion UI,
@@ -72,7 +80,7 @@ public static class UnitConverter
72
80
/// <returns>Output value as the result of converting to <paramref name="toUnit" />.</returns>
73
81
/// <exception cref="QuantityNotFoundException">No quantities were found that match <paramref name="quantityName" />.</exception>
74
82
/// <exception cref="UnitNotFoundException">No units match the abbreviation.</exception>
75
- /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbrevation .</exception>
83
+ /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbreviation .</exception>
76
84
public static double ConvertByName ( FromValue fromValue , string quantityName , string fromUnit , string toUnit )
77
85
{
78
86
if ( ! TryGetQuantityType ( quantityName , out var quantityType ) )
@@ -219,7 +227,7 @@ public static double ConvertByAbbreviation(FromValue fromValue, string quantityN
219
227
/// <returns>Output value as the result of converting to <paramref name="toUnitAbbrev" />.</returns>
220
228
/// <exception cref="QuantityNotFoundException">No quantity types match the <paramref name="quantityName"/>.</exception>
221
229
/// <exception cref="UnitNotFoundException">No unit types match the prefix of <paramref name="quantityName"/> or no units are mapped to the abbreviation.</exception>
222
- /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbrevation .</exception>
230
+ /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbreviation .</exception>
223
231
public static double ConvertByAbbreviation ( FromValue fromValue , string quantityName , string fromUnitAbbrev , string toUnitAbbrev , string culture )
224
232
{
225
233
if ( ! TryGetQuantityType ( quantityName , out var quantityType ) )
@@ -384,37 +392,30 @@ private static bool HasParameterTypes(MethodInfo methodInfo, params Type[] expec
384
392
private static bool TryParseUnit ( Type unitType , string unitName , out object unitValue )
385
393
{
386
394
unitValue = null ;
387
-
388
- if ( ! Enum . IsDefined ( unitType , unitName ) )
395
+ var eNames = Enum . GetNames ( unitType ) ;
396
+ unitName = eNames . FirstOrDefault ( x => x . Equals ( unitName , StringComparison . OrdinalIgnoreCase ) ) ;
397
+ if ( unitName == null )
389
398
return false ;
390
399
391
400
unitValue = Enum . Parse ( unitType , unitName ) ;
392
- if ( unitValue == null )
393
- return false ;
394
-
395
401
return true ;
396
402
}
397
403
398
404
private static bool TryGetUnitType ( string quantityName , out Type unitType )
399
405
{
400
- string unitTypeName = $ " { UnitTypeNamespace } . { quantityName } Unit";
406
+ var unitTypeName = quantityName + " Unit"; // ex. LengthUnit
401
407
402
- unitType = UnitsNetAssembly . GetType ( unitTypeName ) ; // ex: UnitsNet.Units.LengthUnit enum
403
- if ( unitType == null )
404
- return false ;
408
+ unitType = UnitTypes . FirstOrDefault ( x =>
409
+ x . Name . Equals ( unitTypeName , StringComparison . OrdinalIgnoreCase ) ) ;
405
410
406
- return true ;
411
+ return unitType != null ;
407
412
}
408
413
409
414
private static bool TryGetQuantityType ( string quantityName , out Type quantityType )
410
415
{
411
- string quantityTypeName = $ " { QuantityNamespace } . { quantityName } " ;
416
+ quantityType = QuantityTypes . FirstOrDefault ( x => x . Name . Equals ( quantityName , StringComparison . OrdinalIgnoreCase ) ) ;
412
417
413
- quantityType = UnitsNetAssembly . GetType ( quantityTypeName ) ; // ex: UnitsNet.Length struct
414
- if ( quantityType == null )
415
- return false ;
416
-
417
- return true ;
418
+ return quantityType != null ;
418
419
}
419
420
}
420
421
}
0 commit comments