20
20
// THE SOFTWARE.
21
21
22
22
using System ;
23
- using System . Collections . Generic ;
24
23
using System . Globalization ;
25
24
using System . Linq ;
26
25
using System . Reflection ;
@@ -41,17 +40,16 @@ namespace UnitsNet
41
40
/// </summary>
42
41
public static class UnitConverter
43
42
{
44
- private static readonly string QuantityNamespace = typeof ( Length ) . Namespace ;
45
43
private static readonly string UnitTypeNamespace = typeof ( LengthUnit ) . Namespace ;
46
44
private static readonly Assembly UnitsNetAssembly = typeof ( Length ) . GetAssembly ( ) ;
47
45
48
46
private static readonly Type [ ] QuantityTypes = UnitsNetAssembly . GetTypes ( )
49
47
. Where ( typeof ( IQuantity ) . IsAssignableFrom )
50
- . Where ( x => x . IsClass || x . IsValueType ) // Future-proofing: we are discussing changing quantities from struct to class
48
+ . Where ( x => x . IsClass ( ) || x . IsValueType ( ) ) // Future-proofing: we are discussing changing quantities from struct to class
51
49
. ToArray ( ) ;
52
50
53
51
private static readonly Type [ ] UnitTypes = UnitsNetAssembly . GetTypes ( )
54
- . Where ( x => x . Namespace == UnitTypeNamespace && x . IsEnum && x . Name . EndsWith ( "Unit" ) )
52
+ . Where ( x => x . Namespace == UnitTypeNamespace && x . IsEnum ( ) && x . Name . EndsWith ( "Unit" ) )
55
53
. ToArray ( ) ;
56
54
57
55
/// <summary>
@@ -82,7 +80,7 @@ public static class UnitConverter
82
80
/// <returns>Output value as the result of converting to <paramref name="toUnit" />.</returns>
83
81
/// <exception cref="QuantityNotFoundException">No quantities were found that match <paramref name="quantityName" />.</exception>
84
82
/// <exception cref="UnitNotFoundException">No units match the abbreviation.</exception>
85
- /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbrevation .</exception>
83
+ /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbreviation .</exception>
86
84
public static double ConvertByName ( FromValue fromValue , string quantityName , string fromUnit , string toUnit )
87
85
{
88
86
if ( ! TryGetQuantityType ( quantityName , out var quantityType ) )
@@ -229,7 +227,7 @@ public static double ConvertByAbbreviation(FromValue fromValue, string quantityN
229
227
/// <returns>Output value as the result of converting to <paramref name="toUnitAbbrev" />.</returns>
230
228
/// <exception cref="QuantityNotFoundException">No quantity types match the <paramref name="quantityName"/>.</exception>
231
229
/// <exception cref="UnitNotFoundException">No unit types match the prefix of <paramref name="quantityName"/> or no units are mapped to the abbreviation.</exception>
232
- /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbrevation .</exception>
230
+ /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbreviation .</exception>
233
231
public static double ConvertByAbbreviation ( FromValue fromValue , string quantityName , string fromUnitAbbrev , string toUnitAbbrev , string culture )
234
232
{
235
233
if ( ! TryGetQuantityType ( quantityName , out var quantityType ) )
@@ -400,32 +398,24 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit
400
398
return false ;
401
399
402
400
unitValue = Enum . Parse ( unitType , unitName ) ;
403
- if ( unitValue == null )
404
- return false ;
405
-
406
401
return true ;
407
402
}
408
403
409
404
private static bool TryGetUnitType ( string quantityName , out Type unitType )
410
405
{
411
- var unitTypeName = quantityName += "Unit" ; // ex. LengthUnit
412
- unitType = UnitTypes . FirstOrDefault ( x =>
413
- x . Name . Equals ( unitTypeName , StringComparison . OrdinalIgnoreCase ) ) ;
406
+ var unitTypeName = quantityName + "Unit" ; // ex. LengthUnit
414
407
415
- if ( unitType == null )
416
- return false ;
408
+ unitType = UnitTypes . FirstOrDefault ( x =>
409
+ x . Name . Equals ( unitTypeName , StringComparison . OrdinalIgnoreCase ) ) ;
417
410
418
- return true ;
411
+ return unitType != null ;
419
412
}
420
413
421
414
private static bool TryGetQuantityType ( string quantityName , out Type quantityType )
422
415
{
423
416
quantityType = QuantityTypes . FirstOrDefault ( x => x . Name . Equals ( quantityName , StringComparison . OrdinalIgnoreCase ) ) ;
424
-
425
- if ( quantityType == null )
426
- return false ;
427
417
428
- return true ;
418
+ return quantityType != null ;
429
419
}
430
420
}
431
421
}
0 commit comments