Skip to content

Commit b9d895d

Browse files
committed
Fix WRC compile error and some cleanup
1 parent 04f233c commit b9d895d

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

UnitsNet/InternalHelpers/ReflectionBridgeExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ internal static bool IsEnum(this Type type)
5151
#endif
5252
}
5353

54+
internal static bool IsClass(this Type type)
55+
{
56+
#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
57+
return type.GetTypeInfo().IsClass;
58+
#else
59+
return type.IsClass;
60+
#endif
61+
}
62+
5463
internal static bool IsValueType(this Type type)
5564
{
5665
#if !(NET40 || NET35 || NET20 || SILVERLIGHT)

UnitsNet/UnitConverter.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
// THE SOFTWARE.
2121

2222
using System;
23-
using System.Collections.Generic;
2423
using System.Globalization;
2524
using System.Linq;
2625
using System.Reflection;
@@ -41,17 +40,16 @@ namespace UnitsNet
4140
/// </summary>
4241
public static class UnitConverter
4342
{
44-
private static readonly string QuantityNamespace = typeof(Length).Namespace;
4543
private static readonly string UnitTypeNamespace = typeof(LengthUnit).Namespace;
4644
private static readonly Assembly UnitsNetAssembly = typeof(Length).GetAssembly();
4745

4846
private static readonly Type[] QuantityTypes = UnitsNetAssembly.GetTypes()
4947
.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
5149
.ToArray();
5250

5351
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"))
5553
.ToArray();
5654

5755
/// <summary>
@@ -82,7 +80,7 @@ public static class UnitConverter
8280
/// <returns>Output value as the result of converting to <paramref name="toUnit" />.</returns>
8381
/// <exception cref="QuantityNotFoundException">No quantities were found that match <paramref name="quantityName" />.</exception>
8482
/// <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>
8684
public static double ConvertByName(FromValue fromValue, string quantityName, string fromUnit, string toUnit)
8785
{
8886
if(!TryGetQuantityType(quantityName, out var quantityType))
@@ -229,7 +227,7 @@ public static double ConvertByAbbreviation(FromValue fromValue, string quantityN
229227
/// <returns>Output value as the result of converting to <paramref name="toUnitAbbrev" />.</returns>
230228
/// <exception cref="QuantityNotFoundException">No quantity types match the <paramref name="quantityName"/>.</exception>
231229
/// <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>
233231
public static double ConvertByAbbreviation(FromValue fromValue, string quantityName, string fromUnitAbbrev, string toUnitAbbrev, string culture)
234232
{
235233
if(!TryGetQuantityType(quantityName, out var quantityType))
@@ -400,32 +398,24 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit
400398
return false;
401399

402400
unitValue = Enum.Parse(unitType, unitName);
403-
if(unitValue == null)
404-
return false;
405-
406401
return true;
407402
}
408403

409404
private static bool TryGetUnitType(string quantityName, out Type unitType)
410405
{
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
414407

415-
if(unitType == null)
416-
return false;
408+
unitType = UnitTypes.FirstOrDefault(x =>
409+
x.Name.Equals(unitTypeName, StringComparison.OrdinalIgnoreCase));
417410

418-
return true;
411+
return unitType != null;
419412
}
420413

421414
private static bool TryGetQuantityType(string quantityName, out Type quantityType)
422415
{
423416
quantityType = QuantityTypes.FirstOrDefault(x => x.Name.Equals(quantityName, StringComparison.OrdinalIgnoreCase));
424-
425-
if(quantityType == null)
426-
return false;
427417

428-
return true;
418+
return quantityType != null;
429419
}
430420
}
431421
}

0 commit comments

Comments
 (0)