From b5fb82e9349b68b2f5223355710e4c48f4f38f46 Mon Sep 17 00:00:00 2001 From: Claus Date: Wed, 6 Jan 2016 12:01:01 +0100 Subject: [PATCH 1/3] Fixes: U4-7655 PropertyValueEditor tests failing Passing in CurrentUICulture to TryParse method, since it is also used for the normalization part. NumberStyles.Number is the default parameter used by the other TryParse overload so should be good to use here too. --- src/Umbraco.Core/ObjectExtensions.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Core/ObjectExtensions.cs b/src/Umbraco.Core/ObjectExtensions.cs index 90d173b49a..37f0fc0ac3 100644 --- a/src/Umbraco.Core/ObjectExtensions.cs +++ b/src/Umbraco.Core/ObjectExtensions.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; +using System.Globalization; using System.IO; using System.Linq; using System.Linq.Expressions; @@ -322,8 +323,9 @@ namespace Umbraco.Core else if (destinationType == typeof(Decimal)) { Decimal value; + var currentUiCulture = System.Threading.Thread.CurrentThread.CurrentUICulture; var input2 = NormalizeNumberDecimalSeparator(input); - return Decimal.TryParse(input2, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + return Decimal.TryParse(input2, NumberStyles.Number, currentUiCulture, out value) ? Attempt.Succeed(value) : Attempt.Fail(); } else if (destinationType == typeof(Version)) { From b4ca4917cc53dfebd52a205f6df8b06fa9cc3bb9 Mon Sep 17 00:00:00 2001 From: Claus Date: Wed, 6 Jan 2016 12:12:29 +0100 Subject: [PATCH 2/3] Updating Double and Single conversion (where NormalizeNumberDecimalSeparator is also used) to include CurrentUICulture. --- src/Umbraco.Core/ObjectExtensions.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Core/ObjectExtensions.cs b/src/Umbraco.Core/ObjectExtensions.cs index 37f0fc0ac3..99918f7416 100644 --- a/src/Umbraco.Core/ObjectExtensions.cs +++ b/src/Umbraco.Core/ObjectExtensions.cs @@ -247,14 +247,16 @@ namespace Umbraco.Core else if (destinationType == typeof(Double)) { Double value; - var input2 = NormalizeNumberDecimalSeparator(input); - return Double.TryParse(input2, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + var currentUiCulture = System.Threading.Thread.CurrentThread.CurrentUICulture; + var input2 = NormalizeNumberDecimalSeparator(input); + return Double.TryParse(input2, NumberStyles.Float | NumberStyles.AllowThousands, currentUiCulture, out value) ? Attempt.Succeed(value) : Attempt.Fail(); } else if (destinationType == typeof(Single)) { Single value; + var currentUiCulture = System.Threading.Thread.CurrentThread.CurrentUICulture; var input2 = NormalizeNumberDecimalSeparator(input); - return Single.TryParse(input2, out value) ? Attempt.Succeed(value) : Attempt.Fail(); + return Single.TryParse(input2, NumberStyles.Float | NumberStyles.AllowThousands, currentUiCulture, out value) ? Attempt.Succeed(value) : Attempt.Fail(); } else if (destinationType == typeof(Char)) { From b1b7598e3ca507ec0411ba73815575cc90cf9f2b Mon Sep 17 00:00:00 2001 From: Claus Date: Wed, 6 Jan 2016 12:17:30 +0100 Subject: [PATCH 3/3] code cleanup. --- src/Umbraco.Core/ObjectExtensions.cs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/Umbraco.Core/ObjectExtensions.cs b/src/Umbraco.Core/ObjectExtensions.cs index 99918f7416..505af5f7f3 100644 --- a/src/Umbraco.Core/ObjectExtensions.cs +++ b/src/Umbraco.Core/ObjectExtensions.cs @@ -1,16 +1,11 @@ using System; using System.Collections; -using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; -using System.IO; using System.Linq; using System.Linq.Expressions; using System.Reflection; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Formatters.Binary; -using System.Security; using System.Xml; namespace Umbraco.Core @@ -52,7 +47,7 @@ namespace Umbraco.Core public static Attempt TryConvertTo(this object input) { var result = TryConvertTo(input, typeof(T)); - if (!result.Success) + if (result.Success == false) { //just try a straight up conversion try @@ -65,7 +60,7 @@ namespace Umbraco.Core return Attempt.Fail(e); } } - return !result.Success ? Attempt.Fail() : Attempt.Succeed((T)result.Result); + return result.Success == false ? Attempt.Fail() : Attempt.Succeed((T)result.Result); } /// @@ -118,7 +113,7 @@ namespace Umbraco.Core } // we've already dealed with nullables, so any other generic types need to fall through - if (!destinationType.IsGenericType) + if (destinationType.IsGenericType == false) { if (input is string) { @@ -339,7 +334,7 @@ namespace Umbraco.Core return null; // we can't decide... } - private readonly static char[] NumberDecimalSeparatorsToNormalize = new[] {'.', ','}; + private static readonly char[] NumberDecimalSeparatorsToNormalize = new[] {'.', ','}; private static string NormalizeNumberDecimalSeparator(string s) { @@ -446,7 +441,7 @@ namespace Umbraco.Core { var props = TypeDescriptor.GetProperties(o); var d = new Dictionary(); - foreach (var prop in props.Cast().Where(x => !ignoreProperties.Contains(x.Name))) + foreach (var prop in props.Cast().Where(x => ignoreProperties.Contains(x.Name) == false)) { var val = prop.GetValue(o); if (val != null) @@ -482,13 +477,13 @@ namespace Umbraco.Core var items = (from object enumItem in enumerable let value = GetEnumPropertyDebugString(enumItem, levels) where value != null select value).Take(10).ToList(); - return items.Count() > 0 + return items.Any() ? "{{ {0} }}".InvariantFormat(String.Join(", ", items)) : null; } var props = obj.GetType().GetProperties(); - if ((props.Count() == 2) && props[0].Name == "Key" && props[1].Name == "Value" && levels > -2) + if ((props.Length == 2) && props[0].Name == "Key" && props[1].Name == "Value" && levels > -2) { try { @@ -504,12 +499,12 @@ namespace Umbraco.Core if (levels > -1) { var items = - from propertyInfo in props + (from propertyInfo in props let value = GetPropertyDebugString(propertyInfo, obj, levels) where value != null - select "{0}={1}".InvariantFormat(propertyInfo.Name, value); + select "{0}={1}".InvariantFormat(propertyInfo.Name, value)).ToArray(); - return items.Count() > 0 + return items.Any() ? "[{0}]:{{ {1} }}".InvariantFormat(obj.GetType().Name, String.Join(", ", items)) : null; }