From d3bdd17d848fe62713e57fd81801520c21bfd62b Mon Sep 17 00:00:00 2001 From: Trevor Von Seggern Date: Tue, 15 Jan 2019 19:38:16 -0700 Subject: [PATCH 1/9] Insensitive case comparison. --- UnitsNet.Tests/UnitConverterTest.cs | 13 ++++++++++ UnitsNet.Tests/UnitParserTests.cs | 17 +++++++++++++ .../CustomCode/UnitValueAbbreviationLookup.cs | 7 +++--- UnitsNet/UnitConverter.cs | 24 +++++++++++++------ 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/UnitsNet.Tests/UnitConverterTest.cs b/UnitsNet.Tests/UnitConverterTest.cs index f656ea3a14..0bc45cdf37 100644 --- a/UnitsNet.Tests/UnitConverterTest.cs +++ b/UnitsNet.Tests/UnitConverterTest.cs @@ -26,6 +26,7 @@ namespace UnitsNet.Tests public class UnitConverterTest { [Theory] + [InlineData(0, 0, "length", "meter", "centimeter")] [InlineData(0, 0, "Length", "Meter", "Centimeter")] [InlineData(100, 1, "Length", "Meter", "Centimeter")] [InlineData(1, 1000, "Mass", "Gram", "Kilogram")] @@ -35,6 +36,18 @@ public void ConvertByName_ConvertsTheValueToGivenUnit(double expectedValue, doub Assert.Equal(expectedValue, UnitConverter.ConvertByName(inputValue, quantityTypeName, fromUnit, toUnit)); } + [Fact] + public void ConvertByName__QuantityCaseInsensitive() + { + Assert.Equal(0, UnitConverter.ConvertByName(0, "length", "Meter", "Centimeter")); + } + + [Fact] + public void ConvertByName__UnitTypeCaseInsensitive() + { + Assert.Equal(0, UnitConverter.ConvertByName(0, "Length", "meter", "Centimeter")); + } + [Theory] [InlineData(1, "UnknownQuantity", "Meter", "Centimeter")] public void ConvertByName_ThrowsQuantityNotFoundExceptionOnUnknownQuantity(double inputValue, string quantityTypeName, string fromUnit, string toUnit) diff --git a/UnitsNet.Tests/UnitParserTests.cs b/UnitsNet.Tests/UnitParserTests.cs index a6d1dafd8f..1dad8e5e15 100644 --- a/UnitsNet.Tests/UnitParserTests.cs +++ b/UnitsNet.Tests/UnitParserTests.cs @@ -41,6 +41,23 @@ public void Parse_ReturnsUnitMappedByCustomAbbreviation(string customAbbreviatio Assert.Equal(expected, actual); } + [Fact] + public void Parse_AbbreviationCaseInsensitive() + { + var abbreviation = "years"; + var expected = DurationUnit.Year365; + var parser = UnitParser.Default; + + var actual = parser.Parse(abbreviation); + + Assert.Equal(expected, actual); + + abbreviation = "Years"; + actual = parser.Parse(abbreviation); + + Assert.Equal(expected, actual); + } + [Fact] public void Parse_UnknownAbbreviationThrowsUnitNotFoundException() { diff --git a/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs b/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs index 28ee6faac8..e03bc9ace8 100644 --- a/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs +++ b/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs @@ -63,10 +63,9 @@ internal List GetAbbreviationsForUnit(int unit) internal List GetUnitsForAbbreviation(string abbreviation) { - if(!abbreviationToUnitMap.TryGetValue(abbreviation, out var units)) - abbreviationToUnitMap[abbreviation] = units = new List(); - - return units.Distinct().ToList(); + return abbreviationToUnitMap + .Where(x => x.Key.Equals(abbreviation, StringComparison.InvariantCultureIgnoreCase)) + .SelectMany(x => x.Value).Distinct().ToList(); } internal void Add(int unit, string abbreviation, bool setAsDefault = false) diff --git a/UnitsNet/UnitConverter.cs b/UnitsNet/UnitConverter.cs index cf5ac3a15a..0e81b960bb 100644 --- a/UnitsNet/UnitConverter.cs +++ b/UnitsNet/UnitConverter.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using System; +using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reflection; @@ -384,8 +385,9 @@ private static bool HasParameterTypes(MethodInfo methodInfo, params Type[] expec private static bool TryParseUnit(Type unitType, string unitName, out object unitValue) { unitValue = null; - - if(!Enum.IsDefined(unitType, unitName)) + var eNames = Enum.GetNames(unitType); + unitName = eNames.FirstOrDefault(x => x.Equals(unitName, StringComparison.InvariantCultureIgnoreCase)); + if(unitName is null) return false; unitValue = Enum.Parse(unitType, unitName); @@ -395,22 +397,30 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit return true; } + private static List UnitTypes = UnitsNetAssembly.ExportedTypes + .Where(x => x.FullName.StartsWith(UnitTypeNamespace)) + .ToList(); + private static bool TryGetUnitType(string quantityName, out Type unitType) { - string unitTypeName = $"{UnitTypeNamespace}.{quantityName}Unit"; + quantityName += "Unit"; + unitType = QuantityTypes.FirstOrDefault(x => + x.Name.Equals(quantityName, StringComparison.InvariantCultureIgnoreCase)); - unitType = UnitsNetAssembly.GetType(unitTypeName); // ex: UnitsNet.Units.LengthUnit enum if(unitType == null) return false; return true; } + private static List QuantityTypes = UnitsNetAssembly.ExportedTypes + .Where(x => x.FullName.StartsWith(QuantityNamespace)) + .ToList(); + private static bool TryGetQuantityType(string quantityName, out Type quantityType) { - string quantityTypeName = $"{QuantityNamespace}.{quantityName}"; - - quantityType = UnitsNetAssembly.GetType(quantityTypeName); // ex: UnitsNet.Length struct + quantityType = QuantityTypes.FirstOrDefault(x => x.Name.Equals(quantityName, StringComparison.InvariantCultureIgnoreCase)); + if(quantityType == null) return false; From 5283cafd8df74b96f64827561bc6bd3990f268c8 Mon Sep 17 00:00:00 2001 From: Trevor Von Seggern Date: Tue, 15 Jan 2019 20:37:43 -0700 Subject: [PATCH 2/9] ExportedTypes to GetTypes() --- UnitsNet/UnitConverter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UnitsNet/UnitConverter.cs b/UnitsNet/UnitConverter.cs index 0e81b960bb..d50c9c4f71 100644 --- a/UnitsNet/UnitConverter.cs +++ b/UnitsNet/UnitConverter.cs @@ -397,7 +397,7 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit return true; } - private static List UnitTypes = UnitsNetAssembly.ExportedTypes + private static List UnitTypes = UnitsNetAssembly.GetTypes() .Where(x => x.FullName.StartsWith(UnitTypeNamespace)) .ToList(); @@ -413,7 +413,7 @@ private static bool TryGetUnitType(string quantityName, out Type unitType) return true; } - private static List QuantityTypes = UnitsNetAssembly.ExportedTypes + private static List QuantityTypes = UnitsNetAssembly.GetTypes() .Where(x => x.FullName.StartsWith(QuantityNamespace)) .ToList(); From 3317a49163c3b59c93b9308e708e0fcdabd724b9 Mon Sep 17 00:00:00 2001 From: Trevor Von Seggern Date: Tue, 15 Jan 2019 20:48:32 -0700 Subject: [PATCH 3/9] Updated string comparison --- UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs | 2 +- UnitsNet/UnitConverter.cs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs b/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs index e03bc9ace8..c6d27deb0b 100644 --- a/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs +++ b/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs @@ -64,7 +64,7 @@ internal List GetAbbreviationsForUnit(int unit) internal List GetUnitsForAbbreviation(string abbreviation) { return abbreviationToUnitMap - .Where(x => x.Key.Equals(abbreviation, StringComparison.InvariantCultureIgnoreCase)) + .Where(x => x.Key.Equals(abbreviation, StringComparison.OrdinalIgnoreCase)) .SelectMany(x => x.Value).Distinct().ToList(); } diff --git a/UnitsNet/UnitConverter.cs b/UnitsNet/UnitConverter.cs index d50c9c4f71..7a4d49c3bf 100644 --- a/UnitsNet/UnitConverter.cs +++ b/UnitsNet/UnitConverter.cs @@ -386,7 +386,7 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit { unitValue = null; var eNames = Enum.GetNames(unitType); - unitName = eNames.FirstOrDefault(x => x.Equals(unitName, StringComparison.InvariantCultureIgnoreCase)); + unitName = eNames.FirstOrDefault(x => x.Equals(unitName, StringComparison.OrdinalIgnoreCase)); if(unitName is null) return false; @@ -405,7 +405,7 @@ private static bool TryGetUnitType(string quantityName, out Type unitType) { quantityName += "Unit"; unitType = QuantityTypes.FirstOrDefault(x => - x.Name.Equals(quantityName, StringComparison.InvariantCultureIgnoreCase)); + x.Name.Equals(quantityName, StringComparison.OrdinalIgnoreCase)); if(unitType == null) return false; @@ -419,7 +419,7 @@ private static bool TryGetUnitType(string quantityName, out Type unitType) private static bool TryGetQuantityType(string quantityName, out Type quantityType) { - quantityType = QuantityTypes.FirstOrDefault(x => x.Name.Equals(quantityName, StringComparison.InvariantCultureIgnoreCase)); + quantityType = QuantityTypes.FirstOrDefault(x => x.Name.Equals(quantityName, StringComparison.OrdinalIgnoreCase)); if(quantityType == null) return false; From 7d3f110379f3d2eb15353373e4ec9d7e9e743977 Mon Sep 17 00:00:00 2001 From: Trevor Von Seggern Date: Wed, 16 Jan 2019 20:46:36 -0700 Subject: [PATCH 4/9] Case comparison - code review response --- UnitsNet.Tests/UnitParserTests.cs | 19 +++++++++++++++--- .../CustomCode/UnitValueAbbreviationLookup.cs | 6 ++++-- UnitsNet/UnitConverter.cs | 20 +++++++++---------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/UnitsNet.Tests/UnitParserTests.cs b/UnitsNet.Tests/UnitParserTests.cs index 1dad8e5e15..d0a554a568 100644 --- a/UnitsNet.Tests/UnitParserTests.cs +++ b/UnitsNet.Tests/UnitParserTests.cs @@ -42,19 +42,32 @@ public void Parse_ReturnsUnitMappedByCustomAbbreviation(string customAbbreviatio } [Fact] - public void Parse_AbbreviationCaseInsensitive() + public void Parse_AbbreviationCaseInsensitive_Lowercase_years() { + // Arrange var abbreviation = "years"; var expected = DurationUnit.Year365; var parser = UnitParser.Default; + // Act var actual = parser.Parse(abbreviation); + // Assert Assert.Equal(expected, actual); + } - abbreviation = "Years"; - actual = parser.Parse(abbreviation); + [Fact] + public void Parse_AbbreviationCaseInsensitive_Uppercase_Years() + { + // Arrange + var abbreviation = "Years"; + var expected = DurationUnit.Year365; + var parser = UnitParser.Default; + + // Act + var actual = parser.Parse(abbreviation); + // Assert Assert.Equal(expected, actual); } diff --git a/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs b/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs index c6d27deb0b..a5c061d6dc 100644 --- a/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs +++ b/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). // https://github.com/angularsen/UnitsNet // // Permission is hereby granted, free of charge, to any person obtaining a copy @@ -65,7 +65,9 @@ internal List GetUnitsForAbbreviation(string abbreviation) { return abbreviationToUnitMap .Where(x => x.Key.Equals(abbreviation, StringComparison.OrdinalIgnoreCase)) - .SelectMany(x => x.Value).Distinct().ToList(); + .SelectMany(x => x.Value) + .Distinct() + .ToList(); } internal void Add(int unit, string abbreviation, bool setAsDefault = false) diff --git a/UnitsNet/UnitConverter.cs b/UnitsNet/UnitConverter.cs index 7a4d49c3bf..eababbc3e1 100644 --- a/UnitsNet/UnitConverter.cs +++ b/UnitsNet/UnitConverter.cs @@ -45,6 +45,14 @@ public static class UnitConverter private static readonly string UnitTypeNamespace = typeof(LengthUnit).Namespace; private static readonly Assembly UnitsNetAssembly = typeof(Length).GetAssembly(); + private static readonly List QuantityTypes = UnitsNetAssembly.GetTypes() + .Where(x => x.FullName.StartsWith(QuantityNamespace)) + .ToList(); + + private static readonly List UnitTypes = UnitsNetAssembly.GetTypes() + .Where(x => x.FullName.StartsWith(UnitTypeNamespace)) + .ToList(); + /// /// Convert between any two quantity units by their names, such as converting a "Length" of N "Meter" to "Centimeter". /// 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 unitValue = null; var eNames = Enum.GetNames(unitType); unitName = eNames.FirstOrDefault(x => x.Equals(unitName, StringComparison.OrdinalIgnoreCase)); - if(unitName is null) + if(unitName == null) return false; unitValue = Enum.Parse(unitType, unitName); @@ -397,14 +405,10 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit return true; } - private static List UnitTypes = UnitsNetAssembly.GetTypes() - .Where(x => x.FullName.StartsWith(UnitTypeNamespace)) - .ToList(); - private static bool TryGetUnitType(string quantityName, out Type unitType) { quantityName += "Unit"; - unitType = QuantityTypes.FirstOrDefault(x => + unitType = UnitTypes.FirstOrDefault(x => x.Name.Equals(quantityName, StringComparison.OrdinalIgnoreCase)); if(unitType == null) @@ -413,10 +417,6 @@ private static bool TryGetUnitType(string quantityName, out Type unitType) return true; } - private static List QuantityTypes = UnitsNetAssembly.GetTypes() - .Where(x => x.FullName.StartsWith(QuantityNamespace)) - .ToList(); - private static bool TryGetQuantityType(string quantityName, out Type quantityType) { quantityType = QuantityTypes.FirstOrDefault(x => x.Name.Equals(quantityName, StringComparison.OrdinalIgnoreCase)); From 4673c9aa02152babad13e27a1bc2902eaca58985 Mon Sep 17 00:00:00 2001 From: Trevor Von Seggern Date: Sun, 20 Jan 2019 15:25:08 -0700 Subject: [PATCH 5/9] Code review response v2 --- UnitsNet.Tests/UnitParserTests.cs | 6 ------ .../CustomCode/UnitValueAbbreviationLookup.cs | 16 +++++++++------- UnitsNet/UnitConverter.cs | 8 ++++---- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/UnitsNet.Tests/UnitParserTests.cs b/UnitsNet.Tests/UnitParserTests.cs index d0a554a568..da2009e2e0 100644 --- a/UnitsNet.Tests/UnitParserTests.cs +++ b/UnitsNet.Tests/UnitParserTests.cs @@ -44,30 +44,24 @@ public void Parse_ReturnsUnitMappedByCustomAbbreviation(string customAbbreviatio [Fact] public void Parse_AbbreviationCaseInsensitive_Lowercase_years() { - // Arrange var abbreviation = "years"; var expected = DurationUnit.Year365; var parser = UnitParser.Default; - // Act var actual = parser.Parse(abbreviation); - // Assert Assert.Equal(expected, actual); } [Fact] public void Parse_AbbreviationCaseInsensitive_Uppercase_Years() { - // Arrange var abbreviation = "Years"; var expected = DurationUnit.Year365; var parser = UnitParser.Default; - // Act var actual = parser.Parse(abbreviation); - // Assert Assert.Equal(expected, actual); } diff --git a/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs b/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs index a5c061d6dc..8ff5197651 100644 --- a/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs +++ b/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs @@ -63,20 +63,22 @@ internal List GetAbbreviationsForUnit(int unit) internal List GetUnitsForAbbreviation(string abbreviation) { - return abbreviationToUnitMap - .Where(x => x.Key.Equals(abbreviation, StringComparison.OrdinalIgnoreCase)) - .SelectMany(x => x.Value) - .Distinct() - .ToList(); + abbreviation = abbreviation.ToLower(); + if(!abbreviationToUnitMap.TryGetValue(abbreviation, out var units)) + abbreviationToUnitMap[abbreviation] = units = new List(); + + return units.Distinct().ToList(); } internal void Add(int unit, string abbreviation, bool setAsDefault = false) { + // abbreviation = abbreviation.ToLower(); + var lower = abbreviation.ToLower(); if(!unitToAbbreviationMap.TryGetValue(unit, out var abbreviationsForUnit)) abbreviationsForUnit = unitToAbbreviationMap[unit] = new List(); - if(!abbreviationToUnitMap.TryGetValue(abbreviation, out var unitsForAbbreviation)) - abbreviationToUnitMap[abbreviation] = unitsForAbbreviation = new List(); + if(!abbreviationToUnitMap.TryGetValue(lower, out var unitsForAbbreviation)) + abbreviationToUnitMap[lower] = unitsForAbbreviation = new List(); abbreviationsForUnit.Remove(abbreviation); unitsForAbbreviation.Remove(unit); diff --git a/UnitsNet/UnitConverter.cs b/UnitsNet/UnitConverter.cs index eababbc3e1..84afca5441 100644 --- a/UnitsNet/UnitConverter.cs +++ b/UnitsNet/UnitConverter.cs @@ -45,13 +45,13 @@ public static class UnitConverter private static readonly string UnitTypeNamespace = typeof(LengthUnit).Namespace; private static readonly Assembly UnitsNetAssembly = typeof(Length).GetAssembly(); - private static readonly List QuantityTypes = UnitsNetAssembly.GetTypes() + private static readonly Type[] QuantityTypes = UnitsNetAssembly.GetTypes() .Where(x => x.FullName.StartsWith(QuantityNamespace)) - .ToList(); + .ToArray(); - private static readonly List UnitTypes = UnitsNetAssembly.GetTypes() + private static readonly Type[] UnitTypes = UnitsNetAssembly.GetTypes() .Where(x => x.FullName.StartsWith(UnitTypeNamespace)) - .ToList(); + .ToArray(); /// /// Convert between any two quantity units by their names, such as converting a "Length" of N "Meter" to "Centimeter". From 21c2f844700be03ae5b89a82b5c8cb5505bee58b Mon Sep 17 00:00:00 2001 From: Trevor Von Seggern Date: Sat, 26 Jan 2019 21:50:32 -0700 Subject: [PATCH 6/9] Code review response v3 --- UnitsNet.Tests/UnitConverterTest.cs | 4 ++-- UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs | 13 ++++++------- UnitsNet/UnitConverter.cs | 4 ++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/UnitsNet.Tests/UnitConverterTest.cs b/UnitsNet.Tests/UnitConverterTest.cs index 0bc45cdf37..75ce44a1b6 100644 --- a/UnitsNet.Tests/UnitConverterTest.cs +++ b/UnitsNet.Tests/UnitConverterTest.cs @@ -37,13 +37,13 @@ public void ConvertByName_ConvertsTheValueToGivenUnit(double expectedValue, doub } [Fact] - public void ConvertByName__QuantityCaseInsensitive() + public void ConvertByName_QuantityCaseInsensitive() { Assert.Equal(0, UnitConverter.ConvertByName(0, "length", "Meter", "Centimeter")); } [Fact] - public void ConvertByName__UnitTypeCaseInsensitive() + public void ConvertByName_UnitTypeCaseInsensitive() { Assert.Equal(0, UnitConverter.ConvertByName(0, "Length", "meter", "Centimeter")); } diff --git a/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs b/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs index 8ff5197651..5cceceee9d 100644 --- a/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs +++ b/UnitsNet/CustomCode/UnitValueAbbreviationLookup.cs @@ -63,22 +63,21 @@ internal List GetAbbreviationsForUnit(int unit) internal List GetUnitsForAbbreviation(string abbreviation) { - abbreviation = abbreviation.ToLower(); - if(!abbreviationToUnitMap.TryGetValue(abbreviation, out var units)) - abbreviationToUnitMap[abbreviation] = units = new List(); + var lowerCaseAbbreviation = abbreviation.ToLower(); + if(!abbreviationToUnitMap.TryGetValue(lowerCaseAbbreviation, out var units)) + abbreviationToUnitMap[lowerCaseAbbreviation] = units = new List(); return units.Distinct().ToList(); } internal void Add(int unit, string abbreviation, bool setAsDefault = false) { - // abbreviation = abbreviation.ToLower(); - var lower = abbreviation.ToLower(); + var lowerCaseAbbreviation = abbreviation.ToLower(); if(!unitToAbbreviationMap.TryGetValue(unit, out var abbreviationsForUnit)) abbreviationsForUnit = unitToAbbreviationMap[unit] = new List(); - if(!abbreviationToUnitMap.TryGetValue(lower, out var unitsForAbbreviation)) - abbreviationToUnitMap[lower] = unitsForAbbreviation = new List(); + if(!abbreviationToUnitMap.TryGetValue(lowerCaseAbbreviation, out var unitsForAbbreviation)) + abbreviationToUnitMap[lowerCaseAbbreviation] = unitsForAbbreviation = new List(); abbreviationsForUnit.Remove(abbreviation); unitsForAbbreviation.Remove(unit); diff --git a/UnitsNet/UnitConverter.cs b/UnitsNet/UnitConverter.cs index 84afca5441..ac271ca900 100644 --- a/UnitsNet/UnitConverter.cs +++ b/UnitsNet/UnitConverter.cs @@ -407,9 +407,9 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit private static bool TryGetUnitType(string quantityName, out Type unitType) { - quantityName += "Unit"; + var quantityTypeName = quantityName += "Unit"; // ex. LengthUnit unitType = UnitTypes.FirstOrDefault(x => - x.Name.Equals(quantityName, StringComparison.OrdinalIgnoreCase)); + x.Name.Equals(quantityTypeName, StringComparison.OrdinalIgnoreCase)); if(unitType == null) return false; From dc6927013dff0dc654ab375114f02900eaa8c975 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Sun, 27 Jan 2019 17:07:30 +0100 Subject: [PATCH 7/9] Rename variable to unitTypeName --- UnitsNet/UnitConverter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UnitsNet/UnitConverter.cs b/UnitsNet/UnitConverter.cs index ac271ca900..3873800a5a 100644 --- a/UnitsNet/UnitConverter.cs +++ b/UnitsNet/UnitConverter.cs @@ -407,9 +407,9 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit private static bool TryGetUnitType(string quantityName, out Type unitType) { - var quantityTypeName = quantityName += "Unit"; // ex. LengthUnit + var unitTypeName = quantityName += "Unit"; // ex. LengthUnit unitType = UnitTypes.FirstOrDefault(x => - x.Name.Equals(quantityTypeName, StringComparison.OrdinalIgnoreCase)); + x.Name.Equals(unitTypeName, StringComparison.OrdinalIgnoreCase)); if(unitType == null) return false; From 04f233cfe80aad76042bcece0d99b66eafd9764c Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Sun, 27 Jan 2019 17:27:30 +0100 Subject: [PATCH 8/9] Fix QuantityTypes and UnitTypes fields They included more types than they should. --- UnitsNet/UnitConverter.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/UnitsNet/UnitConverter.cs b/UnitsNet/UnitConverter.cs index 3873800a5a..69c0f8213f 100644 --- a/UnitsNet/UnitConverter.cs +++ b/UnitsNet/UnitConverter.cs @@ -46,11 +46,12 @@ public static class UnitConverter private static readonly Assembly UnitsNetAssembly = typeof(Length).GetAssembly(); private static readonly Type[] QuantityTypes = UnitsNetAssembly.GetTypes() - .Where(x => x.FullName.StartsWith(QuantityNamespace)) + .Where(typeof(IQuantity).IsAssignableFrom) + .Where(x => x.IsClass || x.IsValueType) // Future-proofing: we are discussing changing quantities from struct to class .ToArray(); private static readonly Type[] UnitTypes = UnitsNetAssembly.GetTypes() - .Where(x => x.FullName.StartsWith(UnitTypeNamespace)) + .Where(x => x.Namespace == UnitTypeNamespace && x.IsEnum && x.Name.EndsWith("Unit")) .ToArray(); /// From b9d895d98204cceb1542baedb1d4a73ea5576667 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Sun, 27 Jan 2019 17:54:52 +0100 Subject: [PATCH 9/9] Fix WRC compile error and some cleanup --- .../ReflectionBridgeExtensions.cs | 9 ++++++ UnitsNet/UnitConverter.cs | 28 ++++++------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/UnitsNet/InternalHelpers/ReflectionBridgeExtensions.cs b/UnitsNet/InternalHelpers/ReflectionBridgeExtensions.cs index 993f389f96..e57aa2429e 100644 --- a/UnitsNet/InternalHelpers/ReflectionBridgeExtensions.cs +++ b/UnitsNet/InternalHelpers/ReflectionBridgeExtensions.cs @@ -51,6 +51,15 @@ internal static bool IsEnum(this Type type) #endif } + internal static bool IsClass(this Type type) + { +#if !(NET40 || NET35 || NET20 || SILVERLIGHT) + return type.GetTypeInfo().IsClass; +#else + return type.IsClass; +#endif + } + internal static bool IsValueType(this Type type) { #if !(NET40 || NET35 || NET20 || SILVERLIGHT) diff --git a/UnitsNet/UnitConverter.cs b/UnitsNet/UnitConverter.cs index 69c0f8213f..6b83b9a630 100644 --- a/UnitsNet/UnitConverter.cs +++ b/UnitsNet/UnitConverter.cs @@ -20,7 +20,6 @@ // THE SOFTWARE. using System; -using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reflection; @@ -41,17 +40,16 @@ namespace UnitsNet /// public static class UnitConverter { - private static readonly string QuantityNamespace = typeof(Length).Namespace; private static readonly string UnitTypeNamespace = typeof(LengthUnit).Namespace; private static readonly Assembly UnitsNetAssembly = typeof(Length).GetAssembly(); private static readonly Type[] QuantityTypes = UnitsNetAssembly.GetTypes() .Where(typeof(IQuantity).IsAssignableFrom) - .Where(x => x.IsClass || x.IsValueType) // Future-proofing: we are discussing changing quantities from struct to class + .Where(x => x.IsClass() || x.IsValueType()) // Future-proofing: we are discussing changing quantities from struct to class .ToArray(); private static readonly Type[] UnitTypes = UnitsNetAssembly.GetTypes() - .Where(x => x.Namespace == UnitTypeNamespace && x.IsEnum && x.Name.EndsWith("Unit")) + .Where(x => x.Namespace == UnitTypeNamespace && x.IsEnum() && x.Name.EndsWith("Unit")) .ToArray(); /// @@ -82,7 +80,7 @@ public static class UnitConverter /// Output value as the result of converting to . /// No quantities were found that match . /// No units match the abbreviation. - /// More than one unit matches the abbrevation. + /// More than one unit matches the abbreviation. public static double ConvertByName(FromValue fromValue, string quantityName, string fromUnit, string toUnit) { if(!TryGetQuantityType(quantityName, out var quantityType)) @@ -229,7 +227,7 @@ public static double ConvertByAbbreviation(FromValue fromValue, string quantityN /// Output value as the result of converting to . /// No quantity types match the . /// No unit types match the prefix of or no units are mapped to the abbreviation. - /// More than one unit matches the abbrevation. + /// More than one unit matches the abbreviation. public static double ConvertByAbbreviation(FromValue fromValue, string quantityName, string fromUnitAbbrev, string toUnitAbbrev, string culture) { if(!TryGetQuantityType(quantityName, out var quantityType)) @@ -400,32 +398,24 @@ private static bool TryParseUnit(Type unitType, string unitName, out object unit return false; unitValue = Enum.Parse(unitType, unitName); - if(unitValue == null) - return false; - return true; } private static bool TryGetUnitType(string quantityName, out Type unitType) { - var unitTypeName = quantityName += "Unit"; // ex. LengthUnit - unitType = UnitTypes.FirstOrDefault(x => - x.Name.Equals(unitTypeName, StringComparison.OrdinalIgnoreCase)); + var unitTypeName = quantityName + "Unit"; // ex. LengthUnit - if(unitType == null) - return false; + unitType = UnitTypes.FirstOrDefault(x => + x.Name.Equals(unitTypeName, StringComparison.OrdinalIgnoreCase)); - return true; + return unitType != null; } private static bool TryGetQuantityType(string quantityName, out Type quantityType) { quantityType = QuantityTypes.FirstOrDefault(x => x.Name.Equals(quantityName, StringComparison.OrdinalIgnoreCase)); - - if(quantityType == null) - return false; - return true; + return quantityType != null; } } }