diff --git a/src/Umbraco.Core/Configuration/CommaDelimitedConfigurationElement.cs b/src/Umbraco.Core/Configuration/CommaDelimitedConfigurationElement.cs index ce43416a25..7cc8b68ff2 100644 --- a/src/Umbraco.Core/Configuration/CommaDelimitedConfigurationElement.cs +++ b/src/Umbraco.Core/Configuration/CommaDelimitedConfigurationElement.cs @@ -63,7 +63,7 @@ namespace Umbraco.Core.Configuration public void Dispose() { - UmbracoSettings.ObjectExtensions.DisposeIfDisposable(_stringEnumerator); + ObjectExtensions.DisposeIfDisposable(_stringEnumerator); } } } diff --git a/src/Umbraco.Core/Configuration/InnerTextConfigurationElement.cs b/src/Umbraco.Core/Configuration/InnerTextConfigurationElement.cs index 0bc94bb157..107f9ecca0 100644 --- a/src/Umbraco.Core/Configuration/InnerTextConfigurationElement.cs +++ b/src/Umbraco.Core/Configuration/InnerTextConfigurationElement.cs @@ -34,7 +34,7 @@ namespace Umbraco.Core.Configuration { get { - var converted = UmbracoSettings.ObjectExtensions.TryConvertTo(RawValue); + var converted = ObjectExtensions.TryConvertTo(RawValue); if (converted.Success == false) throw new InvalidCastException("Could not convert value " + RawValue + " to type " + typeof(T)); return converted.Result; diff --git a/src/Umbraco.Core/Configuration/UmbracoSettings/ObjectExtensions.cs b/src/Umbraco.Core/Configuration/UmbracoSettings/ObjectExtensions.cs deleted file mode 100644 index 614e23ea13..0000000000 --- a/src/Umbraco.Core/Configuration/UmbracoSettings/ObjectExtensions.cs +++ /dev/null @@ -1,210 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; - -namespace Umbraco.Core.Configuration.UmbracoSettings -{ - - internal static class ObjectExtensions - { - - //private static readonly ConcurrentDictionary> ObjectFactoryCache = new ConcurrentDictionary>(); - - public static IEnumerable AsEnumerableOfOne(this T input) - { - return Enumerable.Repeat(input, 1); - } - - public static void DisposeIfDisposable(this object input) - { - var disposable = input as IDisposable; - if (disposable != null) disposable.Dispose(); - } - - /// - /// Provides a shortcut way of safely casting an input when you cannot guarantee the is an instance type (i.e., when the C# AS keyword is not applicable) - /// - /// - /// The input. - /// - public static T SafeCast(this object input) - { - if (ReferenceEquals(null, input) || ReferenceEquals(default(T), input)) return default(T); - if (input is T) return (T)input; - return default(T); - } - - /// - /// Tries to convert the input object to the output type using TypeConverters - /// - /// - /// - /// - public static Attempt TryConvertTo(this object input) - { - var result = TryConvertTo(input, typeof(T)); - if (!result.Success) - { - //just try a straight up conversion - try - { - var converted = (T)input; - return Attempt.Succeed(converted); - } - catch (Exception e) - { - return Attempt.Fail(e); - } - } - return !result.Success ? Attempt.Fail() : Attempt.Succeed((T)result.Result); - } - - /// - /// Tries to convert the input object to the output type using TypeConverters. If the destination type is a superclass of the input type, - /// if will use . - /// - /// The input. - /// Type of the destination. - /// - public static Attempt TryConvertTo(this object input, Type destinationType) - { - if (input == null) return Attempt.Fail(); - - if (destinationType == typeof(object)) return Attempt.Succeed(input); - - if (input.GetType() == destinationType) return Attempt.Succeed(input); - - if (input is string && destinationType.IsEnum) - { - try - { - var output = Enum.Parse(destinationType, (string)input, true); - return Attempt.Succeed(output); - } - catch (Exception e) - { - return Attempt.Succeed(e); - } - } - - if (!destinationType.IsGenericType || destinationType.GetGenericTypeDefinition() != typeof(Nullable<>)) - { - //TODO: Do a check for destination type being IEnumerable and source type implementing IEnumerable with - // the same 'T', then we'd have to find the extension method for the type AsEnumerable() and execute it. - - if (TypeHelper.IsTypeAssignableFrom(destinationType, input.GetType()) - && TypeHelper.IsTypeAssignableFrom(input)) - { - try - { - var casted = Convert.ChangeType(input, destinationType); - return Attempt.Succeed(casted); - } - catch (Exception e) - { - return Attempt.Fail(e); - } - } - } - - var inputConverter = TypeDescriptor.GetConverter(input); - if (inputConverter.CanConvertTo(destinationType)) - { - try - { - var converted = inputConverter.ConvertTo(input, destinationType); - return Attempt.Succeed(converted); - } - catch (Exception e) - { - return Attempt.Fail(e); - } - } - - if (destinationType == typeof(bool)) - { - var boolConverter = new CustomBooleanTypeConverter(); - if (boolConverter.CanConvertFrom(input.GetType())) - { - try - { - var converted = boolConverter.ConvertFrom(input); - return Attempt.Succeed(converted); - } - catch (Exception e) - { - return Attempt.Fail(e); - } - } - } - - var outputConverter = TypeDescriptor.GetConverter(destinationType); - if (outputConverter.CanConvertFrom(input.GetType())) - { - try - { - var converted = outputConverter.ConvertFrom(input); - return Attempt.Succeed(converted); - } - catch (Exception e) - { - return Attempt.Fail(e); - } - } - - - if (TypeHelper.IsTypeAssignableFrom(input)) - { - try - { - var casted = Convert.ChangeType(input, destinationType); - return Attempt.Succeed(casted); - } - catch (Exception e) - { - return Attempt.Fail(e); - } - } - - return Attempt.Fail(); - } - - public static void CheckThrowObjectDisposed(this IDisposable disposable, bool isDisposed, string objectname) - { - //TODO: Localise this exception - if (isDisposed) - throw new ObjectDisposedException(objectname); - } - - /// - /// Turns object into dictionary - /// - /// - /// Properties to ignore - /// - public static IDictionary ToDictionary(this object o, params string[] ignoreProperties) - { - if (o != null) - { - var props = TypeDescriptor.GetProperties(o); - var d = new Dictionary(); - foreach (var prop in props.Cast().Where(x => !ignoreProperties.Contains(x.Name))) - { - var val = prop.GetValue(o); - if (val != null) - { - d.Add(prop.Name, (TVal)val); - } - } - return d; - } - return new Dictionary(); - } - - - - - - } -} \ No newline at end of file