@@ -45,6 +45,14 @@ public static class UnitConverter
45
45
private static readonly string UnitTypeNamespace = typeof ( LengthUnit ) . Namespace ;
46
46
private static readonly Assembly UnitsNetAssembly = typeof ( Length ) . GetAssembly ( ) ;
47
47
48
+ private static readonly List < Type > QuantityTypes = UnitsNetAssembly . GetTypes ( )
49
+ . Where ( x => x . FullName . StartsWith ( QuantityNamespace ) )
50
+ . ToList ( ) ;
51
+
52
+ private static readonly List < Type > UnitTypes = UnitsNetAssembly . GetTypes ( )
53
+ . Where ( x => x . FullName . StartsWith ( UnitTypeNamespace ) )
54
+ . ToList ( ) ;
55
+
48
56
/// <summary>
49
57
/// Convert between any two quantity units by their names, such as converting a "Length" of N "Meter" to "Centimeter".
50
58
/// This is particularly useful for creating things like a generated unit conversion UI,
@@ -387,7 +395,7 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit
387
395
unitValue = null ;
388
396
var eNames = Enum . GetNames ( unitType ) ;
389
397
unitName = eNames . FirstOrDefault ( x => x . Equals ( unitName , StringComparison . OrdinalIgnoreCase ) ) ;
390
- if ( unitName is null )
398
+ if ( unitName == null )
391
399
return false ;
392
400
393
401
unitValue = Enum . Parse ( unitType , unitName ) ;
@@ -397,14 +405,10 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit
397
405
return true ;
398
406
}
399
407
400
- private static List < Type > UnitTypes = UnitsNetAssembly . GetTypes ( )
401
- . Where ( x => x . FullName . StartsWith ( UnitTypeNamespace ) )
402
- . ToList ( ) ;
403
-
404
408
private static bool TryGetUnitType ( string quantityName , out Type unitType )
405
409
{
406
410
quantityName += "Unit" ;
407
- unitType = QuantityTypes . FirstOrDefault ( x =>
411
+ unitType = UnitTypes . FirstOrDefault ( x =>
408
412
x . Name . Equals ( quantityName , StringComparison . OrdinalIgnoreCase ) ) ;
409
413
410
414
if ( unitType == null )
@@ -413,10 +417,6 @@ private static bool TryGetUnitType(string quantityName, out Type unitType)
413
417
return true ;
414
418
}
415
419
416
- private static List < Type > QuantityTypes = UnitsNetAssembly . GetTypes ( )
417
- . Where ( x => x . FullName . StartsWith ( QuantityNamespace ) )
418
- . ToList ( ) ;
419
-
420
420
private static bool TryGetQuantityType ( string quantityName , out Type quantityType )
421
421
{
422
422
quantityType = QuantityTypes . FirstOrDefault ( x => x . Name . Equals ( quantityName , StringComparison . OrdinalIgnoreCase ) ) ;
0 commit comments