@@ -69,13 +69,15 @@ public static class UnitConverter
69
69
/// </param>
70
70
/// <example>double centimeters = ConvertByName(5, "Length", "Meter", "Centimeter"); // 500</example>
71
71
/// <returns>Output value as the result of converting to <paramref name="toUnit" />.</returns>
72
+ /// <exception cref="UnitNotFoundException">No units match the abbreviation.</exception>
73
+ /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbrevation.</exception>
72
74
public static double ConvertByName ( double fromValue , string quantityName , string fromUnit , string toUnit )
73
75
{
74
- Type quantityType = UnitsNetAssembly . GetType ( $ " { QuantityNamespace } . { quantityName } " ) ; // ex: UnitsNet.Length struct
75
- Type unitType = UnitsNetAssembly . GetType ( $ " { UnitTypeNamespace } . { quantityName } Unit" ) ; // ex: UnitsNet.Units.LengthUnit enum
76
+ Type quantityType = GetQuantityType ( quantityName ) ;
77
+ Type unitType = GetUnitType ( quantityName ) ;
76
78
77
- object fromUnitValue = Enum . Parse ( unitType , fromUnit ) ; // ex: LengthUnit.Meter
78
- object toUnitValue = Enum . Parse ( unitType , toUnit ) ; // ex: LengthUnit.Centimeter
79
+ object fromUnitValue = ParseUnit ( unitType , fromUnit ) ; // ex: LengthUnit.Meter
80
+ object toUnitValue = ParseUnit ( unitType , toUnit ) ; // ex: LengthUnit.Centimeter
79
81
80
82
MethodInfo fromMethod = GetStaticFromMethod ( quantityType , unitType ) ; // ex: UnitsNet.Length.From(double inputValue, LengthUnit inputUnit)
81
83
object fromResult = fromMethod . Invoke ( null , new [ ] { fromValue , fromUnitValue } ) ; // ex: Length quantity = UnitsNet.Length.From(5, LengthUnit.Meter)
@@ -117,6 +119,8 @@ public static bool TryConvertByName(double inputValue, string quantityName, stri
117
119
{
118
120
try
119
121
{
122
+ // TODO Reimplement to avoid exceptions where possible, as Try methods are generally recommended for performance and this is cheating
123
+ // https://msdn.microsoft.com/en-us/library/ms229009(v=vs.100).aspx
120
124
result = ConvertByName ( inputValue , quantityName , fromUnit , toUnit ) ;
121
125
return true ;
122
126
}
@@ -187,10 +191,13 @@ public static double ConvertByAbbreviation(double fromValue, string quantityName
187
191
/// <param name="culture">Culture to parse abbreviations with.</param>
188
192
/// <example>double centimeters = ConvertByName(5, "Length", "m", "cm"); // 500</example>
189
193
/// <returns>Output value as the result of converting to <paramref name="toUnitAbbrev" />.</returns>
194
+ /// <exception cref="QuantityNotFoundException">No quantity types match the <paramref name="quantityName"/>.</exception>
195
+ /// <exception cref="UnitNotFoundException">No unit types match the prefix of <paramref name="quantityName"/> or no units are mapped to the abbreviation.</exception>
196
+ /// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbrevation.</exception>
190
197
public static double ConvertByAbbreviation ( double fromValue , string quantityName , string fromUnitAbbrev , string toUnitAbbrev , string culture )
191
198
{
192
- Type quantityType = UnitsNetAssembly . GetType ( $ " { QuantityNamespace } . { quantityName } " ) ; // ex: UnitsNet.Length struct
193
- Type unitType = UnitsNetAssembly . GetType ( $ " { UnitTypeNamespace } . { quantityName } Unit" ) ; // ex: UnitsNet.Units.LengthUnit enum
199
+ Type quantityType = GetQuantityType ( quantityName ) ;
200
+ Type unitType = GetUnitType ( quantityName ) ;
194
201
195
202
UnitSystem unitSystem = UnitSystem . GetCached ( culture ) ;
196
203
object fromUnitValue = unitSystem . Parse ( fromUnitAbbrev , unitType ) ; // ex: ("m", LengthUnit) => LengthUnit.Meter
@@ -270,6 +277,8 @@ public static bool TryConvertByAbbreviation(double fromValue, string quantityNam
270
277
{
271
278
try
272
279
{
280
+ // TODO Reimplement to avoid exceptions where possible, as Try methods are generally recommended for performance and this is cheating
281
+ // https://msdn.microsoft.com/en-us/library/ms229009(v=vs.100).aspx
273
282
result = ConvertByAbbreviation ( fromValue , quantityName , fromUnitAbbrev , toUnitAbbrev , culture ) ;
274
283
return true ;
275
284
}
@@ -321,5 +330,47 @@ private static bool HasParameterTypes(MethodInfo methodInfo, params Type[] expec
321
330
322
331
return true ;
323
332
}
333
+
334
+
335
+ /// <summary>
336
+ /// Parse a unit by the unit enum type <paramref name="unitType" /> and a unit enum value <paramref name="unitName" />>
337
+ /// </summary>
338
+ /// <param name="unitType">Unit type, such as <see cref="LengthUnit" />.</param>
339
+ /// <param name="unitName">Unit name, such as "Meter" corresponding to <see cref="LengthUnit.Meter" />.</param>
340
+ /// <returns>Unit enum value, such as <see cref="LengthUnit.Meter" /> boxed as an object.</returns>
341
+ /// <exception cref="UnitNotFoundException">No unit values match the <paramref name="unitName" />.</exception>
342
+ private static object ParseUnit ( Type unitType , string unitName )
343
+ {
344
+ object unitValue ; // ex: LengthUnit.Meter
345
+ try
346
+ {
347
+ unitValue = Enum . Parse ( unitType , unitName ) ;
348
+ }
349
+ catch ( Exception e )
350
+ {
351
+ var e2 = new UnitNotFoundException ( $ "Unit not found [{ unitName } ].", e ) ;
352
+ e2 . Data [ "unitName" ] = unitName ;
353
+ throw e2 ;
354
+ }
355
+ return unitValue ;
356
+ }
357
+
358
+ private static Type GetUnitType ( string quantityName )
359
+ {
360
+ string unitTypeName = $ "{ UnitTypeNamespace } .{ quantityName } Unit";
361
+ Type unitType = UnitsNetAssembly . GetType ( unitTypeName ) ; // ex: UnitsNet.Units.LengthUnit enum
362
+ if ( unitType == null )
363
+ throw new UnitNotFoundException ( $ "Unit type name not found: { unitTypeName } ") ;
364
+ return unitType ;
365
+ }
366
+
367
+ private static Type GetQuantityType ( string quantityName )
368
+ {
369
+ string quantityTypeName = $ "{ QuantityNamespace } .{ quantityName } ";
370
+ Type quantityType = UnitsNetAssembly . GetType ( quantityTypeName ) ; // ex: UnitsNet.Length struct
371
+ if ( quantityType == null )
372
+ throw new QuantityNotFoundException ( $ "Quantity type name not found: { quantityTypeName } ") ;
373
+ return quantityType ;
374
+ }
324
375
}
325
376
}
0 commit comments