diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/ConversionExtensionPoint.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/ConversionExtensionPoint.cs deleted file mode 100644 index c984a2accabd..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/ConversionExtensionPoint.cs +++ /dev/null @@ -1,28 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using Uno.Extensions; - -namespace Uno.Conversion -{ - internal class ConversionExtensionPoint : ExtensionPoint - { - public ConversionExtensionPoint(object value) - : base(value) - { - } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/CustomConversionStrategy.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/CustomConversionStrategy.cs deleted file mode 100644 index 240261b51fe0..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/CustomConversionStrategy.cs +++ /dev/null @@ -1,43 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Globalization; -using Uno.Extensions; -using System.Reflection; - -namespace Uno.Conversion -{ - internal class CustomConversionStrategy : IConversionStrategy - { - private readonly Func _conversion; - - public CustomConversionStrategy(Func conversion) - { - _conversion = conversion; - } - - public bool CanConvert(object value, Type toType, CultureInfo culture = null) - { - return value is TFrom && typeof(TTo).IsAssignableFrom(toType); - } - - public object Convert(object value, Type toType, CultureInfo culture = null) - { - return _conversion((TFrom)value, culture); - } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/CustomCulturelessConversionStrategy.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/CustomCulturelessConversionStrategy.cs deleted file mode 100644 index 29bf4a661ac4..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/CustomCulturelessConversionStrategy.cs +++ /dev/null @@ -1,43 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Globalization; -using Uno.Extensions; -using System.Reflection; - -namespace Uno.Conversion -{ - internal class CustomCulturelessConversionStrategy : IConversionStrategy - { - private readonly Func _conversion; - - public CustomCulturelessConversionStrategy(Func conversion) - { - _conversion = conversion; - } - - public bool CanConvert(object value, Type toType, CultureInfo culture = null) - { - return value is TFrom && typeof(TTo).IsAssignableFrom(toType); - } - - public object Convert(object value, Type toType, CultureInfo culture = null) - { - return _conversion((TFrom)value); - } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/DefaultConversionExtensions.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/DefaultConversionExtensions.cs deleted file mode 100644 index f040f9760127..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/DefaultConversionExtensions.cs +++ /dev/null @@ -1,98 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Collections.Generic; -using System.Linq; -using System.Globalization; - -namespace Uno.Conversion -{ - internal sealed class DefaultConversionExtensions : IConversionExtensions - { - private readonly List _registrations; - private readonly List _fallbackRegistrations; - - public DefaultConversionExtensions(bool includeDefaultFallbackStrategies = true) - { - _registrations = new List(4); - _fallbackRegistrations = new List(4); - - if (includeDefaultFallbackStrategies) - { - this.RegisterFallbackStrategy(); - this.RegisterFallbackStrategy(); - this.RegisterFallbackStrategy(); - } - } - - public void RegisterStrategy(IConversionStrategy strategy) - { - _registrations.Add(strategy); - } - - public void RegisterFallbackStrategy(IConversionStrategy strategy) - { - _fallbackRegistrations.Add(strategy); - } - - public bool CanConvert(ConversionExtensionPoint extensionPoint, object value, Type toType, CultureInfo culture = null) - { - return GetConversionStrategy(value, toType, culture) != null; - } - - public ConversionExtensionPoint Conversion(object value) - { - return new ConversionExtensionPoint(value); - } - - public object To(ConversionExtensionPoint extensionPoint, Type toType, CultureInfo culture = null) - { - if (extensionPoint == null || extensionPoint.ExtendedValue == null) - { - throw new ArgumentNullException("extensionPoint", "No extended value to convert"); - } - - return To(extensionPoint.ExtendedValue, toType, culture); - } - - public object To(object value, Type toType, CultureInfo culture = null) - { - var strategy = GetConversionStrategy(value, toType, culture); - - if (strategy == null) - { - var message = - string.Format( - CultureInfo.InvariantCulture, - "No strategy registration for conversion '{0}' to '{1}'", - value.GetType().Name, - toType.Name); - - throw new ArgumentOutOfRangeException("extensionPoint", message); - } - - return strategy.Convert(value, toType, culture); - } - - private IConversionStrategy GetConversionStrategy(object value, Type toType, CultureInfo culture) - { - return _registrations - .Concat(_fallbackRegistrations) - .FirstOrDefault(item => item.CanConvert(value, toType, culture)); - } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/EnumConversionStrategy.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/EnumConversionStrategy.cs deleted file mode 100644 index 9c3b76d430c8..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/EnumConversionStrategy.cs +++ /dev/null @@ -1,159 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.ComponentModel; -using System.Globalization; -using System.Reflection; -using Uno.Extensions; - -namespace Uno.Conversion -{ - /// - /// Will convert to and from enum, usually with strings - /// - /// - /// System.ComponentModel.DescriptionAttribute is can be used as result - /// or parsing value, if not empty. - /// A description attribute with "?" is considered default value. - /// - internal sealed class EnumConversionStrategy : IConversionStrategy - { - private static Func _findValue; - - static EnumConversionStrategy() - { - _findValue = SourceFindValue; - _findValue = _findValue.AsLockedMemoized(); - } - - #region IConversionStrategy Members - - public bool CanConvert(object value, Type toType, CultureInfo culture = null) - { -#if HAS_CRIPPLEDREFLECTION - if (toType.GetTypeInfo().IsEnum) -#else - if (toType.IsEnum) -#endif - { - // NOTE: To be tested - var text = _findValue(toType, value as string ?? value.ToString(), culture); - - return text != null; - } - - //TODO Support value == null - return value != null && -#if HAS_CRIPPLEDREFLECTION - value.GetType().GetTypeInfo().IsEnum && -#else - value.GetType().IsEnum && -#endif - toType == typeof(string); - } - - public object Convert(object value, Type toType, CultureInfo culture = null) - { -#if HAS_CRIPPLEDREFLECTION - if (toType.GetTypeInfo().IsEnum) -#else - if (toType.IsEnum) -#endif - { - // NOTE: To be tested - var text = _findValue(toType, value as string ?? value.ToString(), culture); - - return Enum.Parse(toType, text, true); - } - -#if HAS_CRIPPLEDREFLECTION - - return value?.ToString(); ; -#else - else - { - var text = value.ToString(); - - var attribute = value.Reflection().GetDescriptor(text).FindAttribute(); - - return attribute == null ? text : attribute.Description; - } -#endif - } - - #endregion - - private static string SourceFindValue(Type enumType, string text, CultureInfo culture) - { - text = text.Trim(); - FieldInfo unkownFieldInfo = null; - -#if false //!HAS_TYPEINFO && !HAS_CRIPPLEDREFLECTION && !WINDOWS_UWP - var comparisonType = - culture == null - ? StringComparison.InvariantCultureIgnoreCase - : StringComparison.CurrentCultureIgnoreCase; - - var fields = enumType.GetFields(); - using (culture == null ? null : new Localisation.CultureContext(culture)) -#elif !HAS_TYPEINFO && HAS_CRIPPLEDREFLECTION - var comparisonType = - culture == null - ? StringComparison.OrdinalIgnoreCase - : StringComparison.CurrentCultureIgnoreCase; - - var fields = enumType.GetTypeInfo().GetFields(); -#else - var comparisonType = - culture == null - ? StringComparison.OrdinalIgnoreCase - : StringComparison.CurrentCultureIgnoreCase; - - var fields = enumType.GetTypeInfo().DeclaredFields; -#endif - { - foreach (var field in fields) - { - if (field.Name.Equals(text, comparisonType)) - { - return text; - } - -#if !HAS_CRIPPLEDREFLECTION - var attribute = field.GetDescriptor().FindAttribute(); - - if (attribute == null) - { - continue; - } - - if (attribute.Description.Equals(text, comparisonType)) - { - return field.Name; - } - if (attribute.Description == "?") - { - unkownFieldInfo = field; - } -#endif - } - } - - return unkownFieldInfo == null ? null : unkownFieldInfo.Name; - } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/IConversionExtensions.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/IConversionExtensions.cs deleted file mode 100644 index e8ef3dd37f5b..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/IConversionExtensions.cs +++ /dev/null @@ -1,109 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.ComponentModel; -using System.Globalization; - -namespace Uno.Conversion -{ - internal interface IConversionExtensions - { - /// - /// Create extension point (prefer the usage of method extensions) - /// - [EditorBrowsable(EditorBrowsableState.Never)] - ConversionExtensionPoint Conversion(object value); - - /// - /// Register a conversion strategy - /// - void RegisterStrategy(IConversionStrategy strategy); - - /// - /// Register a conversion strategy to be used as fallback - /// - void RegisterFallbackStrategy(IConversionStrategy strategy); - - /// - /// Check if it's possible to do a conversion - /// - [EditorBrowsable(EditorBrowsableState.Never)] - bool CanConvert(ConversionExtensionPoint extensionPoint, object value, Type toType, CultureInfo culture = null); - - /// - /// Initiate the conversion - /// - /// - /// This method is usually called by the extension point. - /// - /// Conversion result - [EditorBrowsable(EditorBrowsableState.Never)] - object To(ConversionExtensionPoint extensionPoint, Type toType, CultureInfo culture = null); - } - - internal static class ConversionExtensionsExtensions - { - /// - /// Fluently register a conversion strategy - /// - public static IConversionExtensions RegisterStrategy(this IConversionExtensions conversionExtensions) where T : IConversionStrategy, new() - { - conversionExtensions.RegisterStrategy(new T()); - return conversionExtensions; - } - - /// - /// Fluently register a custom conversion strategy - /// - public static IConversionExtensions RegisterCustomStrategy( - this IConversionExtensions conversionExtensions, Func conversion) - { - conversionExtensions.RegisterStrategy(new CustomConversionStrategy(conversion)); - return conversionExtensions; - } - - /// - /// Fluently register a custom conversion strategy - /// - public static IConversionExtensions RegisterCustomStrategy( - this IConversionExtensions conversionExtensions, Func conversion) - { - conversionExtensions.RegisterStrategy(new CustomCulturelessConversionStrategy(conversion)); - return conversionExtensions; - } - - /// - /// Fluently register a conversion strategy to be used as fallback - /// - public static IConversionExtensions RegisterFallbackStrategy(this IConversionExtensions conversionExtensions) where T : IConversionStrategy, new() - { - conversionExtensions.RegisterFallbackStrategy(new T()); - return conversionExtensions; - } - - /// - /// Convert source to a specified generic type - /// - /// Conversion result - public static T To(this IConversionExtensions conversionExtensions, ConversionExtensionPoint extensionPoint, CultureInfo culture = null) - { - return (T)conversionExtensions.To(extensionPoint, typeof(T), culture); - } - - //public static IConversionExtensions RegisterStrategy - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/IConversionStrategy.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/IConversionStrategy.cs deleted file mode 100644 index c42a6f2e4492..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/IConversionStrategy.cs +++ /dev/null @@ -1,36 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Globalization; - -namespace Uno.Conversion -{ - internal interface IConversionStrategy - { - /// - /// If the strategy can support a specific value as input - /// - /// true=supported - bool CanConvert(object value, Type toType, CultureInfo culture = null); - - /// - /// Issue the conversion result - /// - /// result - object Convert(object value, Type toType, CultureInfo culture = null); - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/PrimitiveConversionStrategy.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/PrimitiveConversionStrategy.cs deleted file mode 100644 index eb39f5f72875..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/PrimitiveConversionStrategy.cs +++ /dev/null @@ -1,80 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Reflection; -using System.Text; - -#if NET6_0_OR_GREATER && __IOS__ -using ObjCRuntime; -#endif - -namespace Uno.Conversion -{ - internal class PrimitiveConversionStrategy : IConversionStrategy - { - public bool CanConvert(object value, Type toType, CultureInfo culture = null) - { - var targetNullableType = Nullable.GetUnderlyingType(toType); - if (targetNullableType != null) - { - return CanConvert(value, targetNullableType, culture); - } - - var isTargetPrimitive = toType -#if WINDOWS_UWP - .GetTypeInfo() -#endif - .IsPrimitive || toType == typeof(string); - bool isSourcePrimitive = IsPrimitive(value); - - return isTargetPrimitive && isSourcePrimitive; - } - - private static bool IsPrimitive(object value) - { - var isSourcePrimitive = value.GetType() -#if WINDOWS_UWP - .GetTypeInfo() -#endif - .IsPrimitive - || value is string -#if XAMARIN_IOS - // Those are platform primitives provided for 64 bits compatibility - // with iOS 8.0 and later - || value is nfloat - || value is nint - || value is nuint -#endif - ; - return isSourcePrimitive; - } - - public object Convert(object value, Type toType, CultureInfo culture = null) - { - var targetNullableType = Nullable.GetUnderlyingType(toType); - if (targetNullableType != null) - { - return Convert(value, targetNullableType, culture); - } - - return System.Convert.ChangeType(value, toType, culture ?? CultureInfo.InvariantCulture); - } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/TypeConverterConversionStrategy.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/TypeConverterConversionStrategy.cs deleted file mode 100644 index a162b42301f7..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Conversion/TypeConverterConversionStrategy.cs +++ /dev/null @@ -1,99 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Reflection; -using Uno.Extensions; - -namespace Uno.Conversion -{ - internal class TypeConverterConversionStrategy : IConversionStrategy - { - #region IConversionStrategy Members - - public bool CanConvert(object value, Type toType, CultureInfo culture = null) - { - if (value == null) - { - return true; - } - -#if HAS_NOTYPEDESCRIPTOR || WINDOWS_UWP - return false; -#else - return TypeDescriptor.GetConverter(value.GetType()).CanConvertTo(toType) || - TypeDescriptor.GetConverter(toType).CanConvertFrom(value.GetType()); -#endif - } - - public object Convert(object value, Type toType, CultureInfo culture = null) - { - if (value == null) - { - return null; - } - -#if HAS_NOTYPEDESCRIPTOR || WINDOWS_UWP - throw new InvalidOperationException("TypeConverterConversionStrategy should never return true for CanConvert under WinRT."); -#else - var valueTypeConverter = TypeDescriptor.GetConverter(value.GetType()); - - var canconvert = valueTypeConverter.CanConvertTo(toType); - if (canconvert) - { - return valueTypeConverter.ConvertTo(null, culture, value, toType); - } - else - { - if (toType == typeof(float)) - { - float fValue; - if (float.TryParse(value.ToString(), NumberStyles.Float, culture, out fValue)) - { - return fValue; - } - - if (float.TryParse(value.ToString(), NumberStyles.Float, CultureInfo.InvariantCulture, out fValue)) - { - return fValue; - } - } - - if (toType == typeof(decimal)) - { - decimal dValue; - if (decimal.TryParse(value.ToString(), NumberStyles.AllowDecimalPoint, culture, out dValue)) - { - return dValue; - } - if (decimal.TryParse(value.ToString(), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out dValue)) - { - return dValue; - } - } - - return TypeDescriptor.GetConverter(toType).ConvertFrom(null, culture, value); - } -#endif - - } - - #endregion - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/BindingBehavior.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/BindingBehavior.cs deleted file mode 100644 index 0bac02cbadd4..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/BindingBehavior.cs +++ /dev/null @@ -1,28 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; - -namespace Uno.Reflection -{ - [Flags] - internal enum BindingBehavior - { - Inherited = 1, - Interface = 2, - All = Inherited | Interface - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/BindingContract.Metro.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/BindingContract.Metro.cs deleted file mode 100644 index 6335f13ab79b..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/BindingContract.Metro.cs +++ /dev/null @@ -1,70 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -#if HAS_CRIPPLEDREFLECTION && !XAMARIN -using System; -using System.Reflection; -using Uno.Contracts; - -namespace Uno.Reflection -{ - internal class BindingContract : IContract - { - internal static readonly BindingContract Default; - - internal static readonly BindingContract DefaultIgnoreCase; - - static BindingContract() - { - Default = new BindingContract(); - DefaultIgnoreCase = new BindingContract(); - } - - public BindingContract() - { - } - - public MemberTypes MemberType { get; set; } - - public Type ReturnType { get; set; } - - public Type[] SafeTypes - { - get { return Types ?? new Type[0]; } - } - - public Type[] Types { get; set; } - - public BindingBehavior Behavior { get; set; } - } - - // Summary: - // Marks each type of member that is defined as a derived class of MemberInfo. - [Flags] - internal enum MemberTypes - { - Constructor = 1, - Event = 2, - Field = 4, - Method = 8, - Property = 16, - TypeInfo = 32, - Custom = 64, - NestedType = 128, - All = 191, - } -} -#endif diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/BindingContract.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/BindingContract.cs deleted file mode 100644 index e7fe2cff98e1..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/BindingContract.cs +++ /dev/null @@ -1,83 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -#if !HAS_CRIPPLEDREFLECTION || XAMARIN -using System; -using System.Reflection; -using Uno.Contracts; - -namespace Uno.Reflection -{ - internal class BindingContract : IContract - { - internal static readonly BindingContract Default; - - //internal static readonly BindingContract DefaultIgnoreCase; - - public static BindingFlags DefaultBindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Static | - BindingFlags.Instance | BindingFlags.Public | - BindingFlags.NonPublic; - - public static BindingFlags DefaultBindingFlagsIgnoreCase = DefaultBindingFlags | BindingFlags.IgnoreCase; - - static BindingContract() - { - Default = new BindingContract(DefaultBindingFlags); - //DefaultIgnoreCase = new BindingContract(DefaultBindingFlagsIgnoreCase); - } - - public BindingContract() - : this(DefaultBindingFlags) - { - } - - public BindingContract(BindingFlags bindingFlags) - : this(bindingFlags, null) - { - } - - public BindingContract(BindingFlags bindingFlags, Type[] types) - : this(MemberTypes.All, bindingFlags, null, types, BindingBehavior.All) - { - } - - public BindingContract(MemberTypes memberType, BindingFlags bindingFlags, Type returnType, Type[] types, - BindingBehavior behavior) - { - MemberType = memberType; - BindingFlags = bindingFlags; - ReturnType = returnType; - Types = types; - Behavior = behavior; - } - - public MemberTypes MemberType { get; set; } - - public BindingFlags BindingFlags { get; set; } - - public Type ReturnType { get; set; } - - public Type[] SafeTypes - { - get { return Types ?? Array.Empty(); } - } - - public Type[] Types { get; set; } - - public BindingBehavior Behavior { get; set; } - } -} -#endif diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/DefaultReflectionExtensions.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/DefaultReflectionExtensions.cs deleted file mode 100644 index 370b364ca14f..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/DefaultReflectionExtensions.cs +++ /dev/null @@ -1,282 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Uno.Extensions; -using System.Linq.Expressions; - -namespace Uno.Reflection -{ - internal class DefaultReflectionExtensions : IReflectionExtensions - { - #region IReflectionExtensions Members - - public virtual object Get(IReflectionExtensionPoint extensionPoint, string memberName) - { - return Get(extensionPoint, memberName); - } - - public virtual T Get(IReflectionExtensionPoint extensionPoint, string memberName) - { - return (T)FindValueDescriptor(extensionPoint, memberName).GetValue(extensionPoint.ExtendedValue); - } - - public virtual object Get(IReflectionExtensionPoint extensionPoint, IEnumerable memberNames) - { - return Get(extensionPoint, memberNames); - } - - public virtual T Get(IReflectionExtensionPoint extensionPoint, IEnumerable memberNames) - { - return Get(extensionPoint, GetDescriptors(extensionPoint, memberNames).Cast()); - } - - public virtual object Get(IReflectionExtensionPoint extensionPoint, - IEnumerable descriptors) - { - return Get(extensionPoint, descriptors); - } - - public virtual T Get(IReflectionExtensionPoint extensionPoint, - IEnumerable descriptors) - { - var value = extensionPoint.ExtendedValue; - - descriptors.ForEach(item => value = item.GetValue(value)); - - return (T)value; - } - - public virtual void Set(IReflectionExtensionPoint extensionPoint, string memberName, T value) - { - FindValueDescriptor(extensionPoint, memberName).SetValue(extensionPoint.ExtendedValue, value); - } - - public virtual void Set(IReflectionExtensionPoint extensionPoint, IEnumerable memberNames, T value) - { - Set(extensionPoint, GetDescriptors(extensionPoint, memberNames).Cast(), value); - } - - public virtual void Set(IReflectionExtensionPoint extensionPoint, - IEnumerable descriptors, T value) - { - var allButLast = descriptors.Take(descriptors.Count() - 1); - - var last = descriptors.Last(); - - var currentValue = extensionPoint.Get(allButLast); - - currentValue.Reflection().Set(last.MemberInfo.Name, value); - } - - public virtual IEnumerable GetValueDescriptors(IReflectionExtensionPoint extensionPoint, - IEnumerable memberNames) - { - return GetDescriptors(extensionPoint, memberNames).Cast(); - } - - public virtual IEnumerable GetDescriptors(IReflectionExtensionPoint extensionPoint, - IEnumerable memberNames) - { - IMemberDescriptor descriptor = null; - - foreach (var memberName in memberNames) - { - descriptor = descriptor == null - ? FindDescriptor(extensionPoint, memberName) - : FindDescriptor(descriptor, memberName); - - yield return descriptor; - } - } - - public virtual IMemberDescriptor GetDescriptor(IReflectionExtensionPoint extensionPoint) - { -#if !WINDOWS_UWP - return GetDescriptor(extensionPoint.ExtendedType); -#else - return GetDescriptor(extensionPoint.ExtendedType.GetTypeInfo()); -#endif - } - - public virtual IMemberDescriptor GetDescriptor(IMemberDescriptor descriptor, string memberName) - { - var item = FindDescriptor(descriptor, memberName); - - return item.Validation().Found(); - } - - public virtual IMemberDescriptor FindDescriptor(IMemberDescriptor descriptor, string memberName) - { - var mi = descriptor.Type.FindMemberInfo(memberName, BindingContract.Default); - - return mi == null ? null : GetDescriptor(mi); - } - - public virtual IValueMemberDescriptor GetValueDescriptor(IReflectionExtensionPoint extensionPoint, - string memberName) - { - var descriptor = FindValueDescriptor(extensionPoint, memberName); - - return descriptor.Validation().Found(); - } - - public virtual IValueMemberDescriptor FindValueDescriptor(IReflectionExtensionPoint extensionPoint, - string memberName) - { - return (IValueMemberDescriptor)FindDescriptor(extensionPoint, memberName); - } - - public virtual IMemberDescriptor GetDescriptor(IReflectionExtensionPoint extensionPoint, string memberName) - { - var descriptor = FindDescriptor(extensionPoint, memberName); - - return descriptor.Validation().Found(); - } - - public virtual IMemberDescriptor FindDescriptor(IReflectionExtensionPoint extensionPoint, string memberName) - { - return FindDescriptor(extensionPoint, memberName, BindingContract.Default); - } - - public virtual IMemberDescriptor FindDescriptor(IReflectionExtensionPoint extensionPoint, string memberName, - BindingContract contract) - { - var mi = extensionPoint.ExtendedType.FindMemberInfo(memberName, contract); - - return mi == null ? null : GetDescriptor(mi); - } - - public virtual IReflectionExtensionPoint Reflection(Type type) - { - return new ReflectionExtensionPoint(type); - } - - public virtual IReflectionExtensionPoint Reflection(T instance) - { - return new ReflectionExtensionPoint(instance); - } - - public virtual IMemberDescriptor GetDescriptor(MemberInfo mi) - { - var descriptor = FindDescriptor(mi); - - if (descriptor == null) - { - throw new ArgumentException("Not Supported"); - } - return descriptor; - } - - public virtual IMemberDescriptor GetDescriptor(IReflectionExtensionPoint extensionPoint, Expression> func) - { - return GetMemberInfo((LambdaExpression)func); - } - - public virtual IMemberDescriptor GetDescriptor(IReflectionExtensionPoint extensionPoint, Expression> func) - { - return GetMemberInfo((LambdaExpression)func); - } - - - #endregion - - // TODO: rewrite if-else statements - protected virtual IMemberDescriptor FindDescriptor(MemberInfo mi) - { - var propertyInfo = mi as PropertyInfo; - - if (propertyInfo == null) - { - var fieldInfo = mi as FieldInfo; - - if (fieldInfo == null) - { - var methodInfo = mi as MethodInfo; - - if (methodInfo == null) - { - var eventInfo = mi as EventInfo; - - if (eventInfo == null) - { -#if !WINDOWS_UWP && !HAS_CRIPPLEDREFLECTION - var type = mi as Type; - - if (type == null) - { - return null; - } - else - { -#if !SILVERLIGHT && !WINDOWS_PHONE - if (type.IsNested) - { - return new NestedTypeDescriptor(type); - } - else -#endif - { - return new TypeDescriptor(type); - } - } -#else - return null; -#endif - } - else - { - return new EventDescriptor(eventInfo); - } - } - else - { - return null; - } - } - else - { -#if !HAS_CRIPPLEDREFLECTION - return new FieldDescriptor(fieldInfo); -#else - return null; -#endif - } - } - else - { - return new PropertyDescriptor(propertyInfo); - } - } - - private IMemberDescriptor GetMemberInfo(LambdaExpression func) - { - MethodCallExpression call = func.Body as MethodCallExpression; - - MemberExpression member = func.Body as MemberExpression; - - if (member != null) - { - return FindDescriptor(member.Member); - } - - throw new ArgumentException("The expression must call a method, a property or get a field at the first level."); - } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/EventDescriptor.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/EventDescriptor.cs deleted file mode 100644 index fb36deeecb0f..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/EventDescriptor.cs +++ /dev/null @@ -1,57 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Reflection; -using Uno.Extensions; -using System.Collections.Generic; - -namespace Uno.Reflection -{ - internal class EventDescriptor : MemberDescriptor, IEventDescriptor - { - private Dictionary descriptors = new Dictionary(); - - public EventDescriptor(EventInfo eventInfo) - : base(eventInfo) - { - } - - #region IEventDescriptor Members - - public override Type Type - { - get { return MemberInfo.EventHandlerType; } - } - - public override bool IsStatic - { - get { return Add.IsStatic; } - } - - public IMethodDescriptor Add - { - get { return descriptors.FindOrCreate("Add", () => (IMethodDescriptor)MemberInfo.GetAddMethod(true).GetDescriptor()); } - } - - public IMethodDescriptor Remove - { - get { return descriptors.FindOrCreate("Remove", () => (IMethodDescriptor)MemberInfo.GetRemoveMethod(true).GetDescriptor()); } - } - - #endregion - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/FieldDescriptor.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/FieldDescriptor.cs deleted file mode 100644 index fb603280fc50..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/FieldDescriptor.cs +++ /dev/null @@ -1,191 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -#if !HAS_CRIPPLEDREFLECTION -using System; -using System.Reflection; -using System.Linq; -using System.Reflection.Emit; -using Uno.Reflection; -using Uno.Extensions; - -namespace Uno.Reflection -{ - internal class FieldDescriptor : ValueMemberDescriptor - { - public FieldDescriptor(FieldInfo fi) - : base(fi) - { - } - - public override Type Type => MemberInfo.FieldType; - - public override bool IsStatic => MemberInfo.IsStatic; - - public override object GetValue(object instance) => MemberInfo.GetValue(instance); - - public override void SetValue(object instance, object value) => MemberInfo.SetValue(instance, value); - - /// - /// Creates a compiled method that will allow a the assignation of the specified field. - /// - /// The field to assign - /// A delegate taking an instance as the first parameter, and the value as the second parameter. - public override Action ToCompiledSetValue() => ToCompiledSetValue(MemberInfo.DeclaringType.TypeHandle, MemberInfo.FieldHandle, true); - - /// - /// Creates a compiled method that will allow a the assignation of the specified field. - /// - /// The field to assign - /// Removes some type checking to enhance performance if set to false. - /// A delegate taking an instance as the first parameter, and the value as the second parameter. - /// - /// The use of the strict parameter is required if the caller of the generated method does not validate - /// parameter types before the call. Invalid parameters could result in unexpected behavior. - /// - public override Action ToCompiledSetValue(bool strict) => ToCompiledSetValue(MemberInfo.DeclaringType.TypeHandle, MemberInfo.FieldHandle, strict); - - /// - /// Creates a compiled method that will allow a the assignation of the specified field. - /// - /// The declaring type for the specified RuntimeFieldHandle - /// The field in the specified RuntimeTypeHandle - /// Removes some type checking to enhance performance. - /// A delegate taking an instance as the first parameter, and the value as the second parameter. - /// - /// The use of the strict parameter is required if the caller of the generated method does not validate - /// parameter types before the call. Invalid parameters could result in unexpected behavior. - /// - public static Action ToCompiledSetValue(RuntimeTypeHandle typeHandle, RuntimeFieldHandle fieldHandle, bool strict) - { -#if NET6_0_OR_GREATER - var fieldInfo = FieldInfo.GetFieldFromHandle(fieldHandle, typeHandle); - - if (fieldInfo.IsStatic || fieldInfo.DeclaringType.IsValueType) - { - // Don't compile code for static fields - return fieldInfo.SetValue; - } - - var name = "Set_{0}.{1}-{2}".InvariantCultureFormat(fieldInfo.DeclaringType.Name, fieldInfo.Name, Guid.NewGuid()); - - var method = new DynamicMethod(name, typeof(void), new[] { typeof(object), typeof(object) }, typeof(FieldDescriptor), true); - - var il = method.GetILGenerator(); - - il.Emit(OpCodes.Ldarg_0); - - if (strict) - { - il.Emit(OpCodes.Castclass, fieldInfo.DeclaringType); - } - - il.Emit(OpCodes.Ldarg_1); - - if (fieldInfo.FieldType.IsValueType) - { - il.Emit(OpCodes.Unbox_Any, fieldInfo.FieldType); - } - else - { - if (strict) - { - il.Emit(OpCodes.Castclass, fieldInfo.FieldType); - } - } - - il.Emit(OpCodes.Stfld, fieldInfo); - il.Emit(OpCodes.Ret); - - return method.CreateDelegate(typeof(Action)) as Action; -#else - throw new NotSupportedException($"ToCompiledSetValue is not supported on this platform"); -#endif - } - - /// - /// Creates a compiled method that will get the value of a field - /// - /// The field to get the value from. - /// - public override Func ToCompiledGetValue() => ToCompiledGetValue(MemberInfo.DeclaringType.TypeHandle, MemberInfo.FieldHandle, true); - - /// - /// Creates a compiled method that will get the value of a field. - /// - /// The field to get the value from. - /// Removes some type checking to enhance performance if set to false. - /// A delegate taking an instance as the first parameter, and returns the value of the field. - /// - /// The use of the strict parameter is required if the caller of the generated method does not validate - /// parameter types before the call. An invalid parameter could result in unexpected behavior. - /// - public override Func ToCompiledGetValue(bool strict) => ToCompiledGetValue(MemberInfo.DeclaringType.TypeHandle, MemberInfo.FieldHandle, strict); - - /// - /// Creates a compiled method that will get the value of a field. - /// - /// The declaring type for the specified RuntimeFieldHandle - /// The field in the specified RuntimeTypeHandle - /// Removes some type checking to enhance performance if set to false. - /// - /// - /// The use of the strict parameter is required if the caller of the generated method does not validate - /// parameter types before the call. An invalid parameter could result in unexpected behavior. - /// - public static Func ToCompiledGetValue(RuntimeTypeHandle typeHandle, RuntimeFieldHandle fieldHandle, bool strict) - { -#if NET6_0_OR_GREATER - var fieldInfo = FieldInfo.GetFieldFromHandle(fieldHandle, typeHandle); - - if (fieldInfo.IsStatic || fieldInfo.DeclaringType.IsValueType) - { - // Don't compile code for static fields or fields from value types - return fieldInfo.GetValue; - } - - var name = "Get_{0}.{1}-{2}".InvariantCultureFormat(fieldInfo.DeclaringType.Name, fieldInfo.Name, Guid.NewGuid()); - - var method = new DynamicMethod(name, typeof(object), new[] { typeof(object) }, typeof(FieldDescriptor), true); - - var il = method.GetILGenerator(); - - il.DeclareLocal(typeof(object)); - - il.Emit(OpCodes.Ldarg_0); - - if (strict) - { - il.Emit(OpCodes.Castclass, fieldInfo.DeclaringType); - } - - il.Emit(OpCodes.Ldfld, fieldInfo); - - if (fieldInfo.FieldType.IsValueType) - { - il.Emit(OpCodes.Box, fieldInfo.FieldType); - } - - il.Emit(OpCodes.Ret); - - return method.CreateDelegate(typeof(Func)) as Func; -#else - throw new NotSupportedException($"ToCompiledSetValue is not supported on this platform"); -#endif - } - } -} -#endif diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IContract.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IContract.cs deleted file mode 100644 index cd53180685de..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IContract.cs +++ /dev/null @@ -1,22 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -namespace Uno.Contracts -{ - internal interface IContract - { - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IEventDescriptor.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IEventDescriptor.cs deleted file mode 100644 index 51e8181201bf..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IEventDescriptor.cs +++ /dev/null @@ -1,24 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -namespace Uno.Reflection -{ - internal interface IEventDescriptor : IMemberDescriptor - { - IMethodDescriptor Add { get; } - IMethodDescriptor Remove { get; } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IMemberDescriptor.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IMemberDescriptor.cs deleted file mode 100644 index 0a6a3a44fd95..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IMemberDescriptor.cs +++ /dev/null @@ -1,39 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Reflection; - -namespace Uno.Reflection -{ - internal interface IMemberDescriptor - { - Type Type { get; } - - MemberInfo MemberInfo { get; } - - bool IsStatic { get; } - bool IsInstance { get; } - - bool IsGeneric { get; } - - bool IsOpen { get; } - bool IsClosed { get; } - - IMemberDescriptor Open(); - IMemberDescriptor Close(params Type[] types); - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IMethodDescriptor.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IMethodDescriptor.cs deleted file mode 100644 index b4ef8594a3d6..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IMethodDescriptor.cs +++ /dev/null @@ -1,43 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -namespace Uno.Reflection -{ - internal interface IMethodDescriptor : IMemberDescriptor - { - object Invoke(object instance, params object[] args); - - /// - /// Build a compiled method that will call the specified method. - /// - /// The method to invoke - /// A delegate that will call the requested method - Func ToCompiledMethodInvoke(); - - /// - /// Build a compiled method that will call the specified method. - /// - /// The method to invoke - /// Removes some type checking to enhance performance if set to false. - /// A delegate that will call the requested method - /// - /// The use of the strict parameter is required if the caller of the generated method does not validate - /// parameter types before the call. An invalid parameter could result in unexpected behavior. - /// - Func ToCompiledMethodInvoke(bool strict); - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IReflectionExtensionPoint.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IReflectionExtensionPoint.cs deleted file mode 100644 index ceb3be2b978f..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IReflectionExtensionPoint.cs +++ /dev/null @@ -1,28 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using Uno.Extensions; - -namespace Uno.Reflection -{ - internal interface IReflectionExtensionPoint : IExtensionPoint - { - } - - internal interface IReflectionExtensionPoint : IReflectionExtensionPoint - { - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IReflectionExtensions.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IReflectionExtensions.cs deleted file mode 100644 index 74504454ad04..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IReflectionExtensions.cs +++ /dev/null @@ -1,67 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Linq.Expressions; - -namespace Uno.Reflection -{ - internal interface IReflectionExtensions - { - IReflectionExtensionPoint Reflection(Type type); - IReflectionExtensionPoint Reflection(T instance); - - object Get(IReflectionExtensionPoint extensionPoint, string memberName); - T Get(IReflectionExtensionPoint extensionPoint, string memberName); - - object Get(IReflectionExtensionPoint extensionPoint, IEnumerable memberNames); - T Get(IReflectionExtensionPoint extensionPoint, IEnumerable memberNames); - - object Get(IReflectionExtensionPoint extensionPoint, IEnumerable descriptors); - T Get(IReflectionExtensionPoint extensionPoint, IEnumerable descriptors); - - void Set(IReflectionExtensionPoint extensionPoint, string memberName, T value); - void Set(IReflectionExtensionPoint extensionPoint, IEnumerable memberNames, T value); - void Set(IReflectionExtensionPoint extensionPoint, IEnumerable descriptors, T value); - - IEnumerable GetValueDescriptors(IReflectionExtensionPoint extensionPoint, - IEnumerable memberNames); - - IEnumerable GetDescriptors(IReflectionExtensionPoint extensionPoint, - IEnumerable memberNames); - - IMemberDescriptor GetDescriptor(IMemberDescriptor descriptor, string memberName); - IMemberDescriptor GetDescriptor(IReflectionExtensionPoint extensionPoint, Expression> func); - IMemberDescriptor GetDescriptor(IReflectionExtensionPoint extensionPoint, Expression> func); - - IMemberDescriptor FindDescriptor(IMemberDescriptor descriptor, string memberName); - - IValueMemberDescriptor GetValueDescriptor(IReflectionExtensionPoint extensionPoint, string memberName); - IValueMemberDescriptor FindValueDescriptor(IReflectionExtensionPoint extensionPoint, string memberName); - - //TODO GetMethodDescriptor + FindMethodDescriptor - IMemberDescriptor GetDescriptor(IReflectionExtensionPoint extensionPoint); - IMemberDescriptor GetDescriptor(IReflectionExtensionPoint extensionPoint, string memberName); - IMemberDescriptor FindDescriptor(IReflectionExtensionPoint extensionPoint, string memberName); - - IMemberDescriptor FindDescriptor(IReflectionExtensionPoint extensionPoint, string memberName, - BindingContract contract); - - IMemberDescriptor GetDescriptor(MemberInfo mi); - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IValueMemberDescriptor.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IValueMemberDescriptor.cs deleted file mode 100644 index 78179e13fb02..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/IValueMemberDescriptor.cs +++ /dev/null @@ -1,61 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -namespace Uno.Reflection -{ - internal interface IValueMemberDescriptor : IMemberDescriptor - { - object GetValue(object instance); - void SetValue(object instance, object value); - - /// - /// Creates a compiled method that will allow a the assignation of the current member. - /// - /// A delegate taking an instance as the first parameter, and the value as the second parameter. - Action ToCompiledSetValue(); - - /// - /// Creates a compiled method that will allow a the assignation of the current member. - /// - /// Removes some type checking to enhance performance if set to false. - /// A delegate taking an instance as the first parameter, and the value as the second parameter. - /// - /// The use of the strict parameter is required if the caller of the generated method does not validate - /// parameter types before the call. Invalid parameters could result in unexpected behavior. - /// - Action ToCompiledSetValue(bool strict); - - /// - /// Creates a compiled method that will get the value of of the current member. - /// - /// The field to get the value from. - /// - Func ToCompiledGetValue(); - - /// - /// Creates a compiled method that will get the value of of the current member. - /// - /// The field to get the value from. - /// Removes some type checking to enhance performance if set to false. - /// A delegate taking an instance as the first parameter, and returns the value of the field. - /// - /// The use of the strict parameter is required if the caller of the generated method does not validate - /// parameter types before the call. An invalid parameter could result in unexpected behavior. - /// - Func ToCompiledGetValue(bool strict); - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/MemberDescriptor.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/MemberDescriptor.cs deleted file mode 100644 index c30f0753be8b..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/MemberDescriptor.cs +++ /dev/null @@ -1,85 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Reflection; -using Uno.Extensions; - -namespace Uno.Reflection -{ - internal abstract class MemberDescriptor : IMemberDescriptor - where TMemberInfo : MemberInfo - { - private readonly TMemberInfo memberInfo; - - // TODO: protected constructor? - public MemberDescriptor(TMemberInfo memberInfo) - { - this.memberInfo = memberInfo.Validation().NotNull("memberInfo"); - } - - public TMemberInfo MemberInfo - { - get { return memberInfo; } - } - - #region IMemberDescriptor Members - - public abstract Type Type { get; } - - MemberInfo IMemberDescriptor.MemberInfo - { - get { return memberInfo; } - } - - public virtual bool IsStatic - { - get { return false; } - } - - public virtual bool IsInstance - { - get { return !IsStatic; } - } - - public virtual bool IsGeneric - { - get { return false; } - } - - public virtual bool IsOpen - { - get { return false; } - } - - public virtual bool IsClosed - { - get { return !IsOpen; } - } - - public virtual IMemberDescriptor Open() - { - throw new InvalidOperationException(); - } - - public virtual IMemberDescriptor Close(params Type[] types) - { - throw new InvalidOperationException(); - } - - #endregion - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/MemberDescriptorExtensions.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/MemberDescriptorExtensions.cs deleted file mode 100644 index 9d04223c805b..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/MemberDescriptorExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Reflection; -using System.Linq; -using Uno.Reflection; - -namespace Uno.Extensions -{ - //TODO Use Extensions Pattern or move to ReflectionExtensions. Or maybe MemberInfoExtensions directly. - internal static class MemberDescriptorExtensions - { - public static T FindAttribute(this IMemberDescriptor descriptor) - where T : Attribute - { - var attributes = descriptor.MemberInfo.GetCustomAttributes(typeof(T), true); - return attributes.FirstOrDefault() as T; - } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/NestedTypeDescriptor.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/NestedTypeDescriptor.cs deleted file mode 100644 index fb88db334d93..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/NestedTypeDescriptor.cs +++ /dev/null @@ -1,91 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -#if !WINDOWS_UWP -using System; - -namespace Uno.Reflection -{ - internal class NestedTypeDescriptor : TypeDescriptor, IValueMemberDescriptor - { - public NestedTypeDescriptor(Type type) - : base(type) - { - } - - #region IValueMemberDescriptor Members - - public override Type Type - { - get { return MemberInfo; } - } - - public override bool IsStatic - { - get { return true; } - } - - public override IMemberDescriptor Open() - { - return IsClosed ? new NestedTypeDescriptor(MemberInfo.GetGenericTypeDefinition()) : base.Open(); - } - - // TODO: params are ignored - public override IMemberDescriptor Close(params Type[] types) - { - if (!IsOpen) - { - return base.Close(types); - } - var closedType = MemberInfo.MakeGenericType(types); - - return new NestedTypeDescriptor(closedType); - } - - public object GetValue(object instance) - { - return instance; - } - - public void SetValue(object instance, object value) - { - throw new NotImplementedException(); - } - - public Action ToCompiledSetValue() - { - return SetValue; - } - - public Action ToCompiledSetValue(bool strict) - { - return SetValue; - } - - public Func ToCompiledGetValue() - { - return GetValue; - } - - public Func ToCompiledGetValue(bool strict) - { - return GetValue; - } - - #endregion - } -} -#endif diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/PropertyDescriptor.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/PropertyDescriptor.cs deleted file mode 100644 index f76e53317a81..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/PropertyDescriptor.cs +++ /dev/null @@ -1,70 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Reflection; -using Uno.Extensions; - -namespace Uno.Reflection -{ - internal class PropertyDescriptor : ValueMemberDescriptor - { - public PropertyDescriptor(PropertyInfo pi) - : base(pi) - { - } - - public override Type Type - { - get { return MemberInfo.PropertyType; } - } - - public override bool IsStatic - { - get { return MemberInfo.GetGetMethod(true).IsStatic; } - } - - public override object GetValue(object instance) - { - return MemberInfo.GetValue(instance, null); - } - - public override void SetValue(object instance, object value) - { - MemberInfo.SetValue(instance, value, null); - } - - public override Func ToCompiledGetValue() - { - return instance => MemberInfo.GetValue(instance, null); - } - - public override Func ToCompiledGetValue(bool strict) - { - return instance => MemberInfo.GetValue(instance, null); - } - - public override Action ToCompiledSetValue() - { - return (instance, value) => MemberInfo.SetValue(instance, value, null); - } - - public override Action ToCompiledSetValue(bool strict) - { - return (instance, value) => MemberInfo.SetValue(instance, value, null); - } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/ReflectionExtensionPoint.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/ReflectionExtensionPoint.cs deleted file mode 100644 index 3fad40e54738..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/ReflectionExtensionPoint.cs +++ /dev/null @@ -1,34 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using Uno.Extensions; - -namespace Uno.Reflection -{ - internal class ReflectionExtensionPoint : ExtensionPoint, IReflectionExtensionPoint, IReflectionExtensionPoint - { - public ReflectionExtensionPoint(Type type) - : base(type) - { - } - - public ReflectionExtensionPoint(T value) - : base(value) - { - } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/ReflectionExtensions.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/ReflectionExtensions.cs deleted file mode 100644 index 4840e6f01c96..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/ReflectionExtensions.cs +++ /dev/null @@ -1,186 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Collections.Generic; -using System.Reflection; -using Uno.Reflection; -using System.Linq.Expressions; - -namespace Uno.Extensions -{ - internal static class ReflectionExtensions - { - public static IReflectionExtensions Extensions { get; } = new DefaultReflectionExtensions(); - - public static object Get(this IReflectionExtensionPoint extensionPoint, string memberName) - { - return Extensions.Get(extensionPoint, memberName); - } - - public static T Get(this IReflectionExtensionPoint extensionPoint, string memberName) - { - return Extensions.Get(extensionPoint, memberName); - } - - public static T Get(this IReflectionExtensionPoint extensionPoint, string memberName) - { - return Extensions.Get(extensionPoint, memberName); - } - - public static object Get(this IReflectionExtensionPoint extensionPoint, IEnumerable memberNames) - { - return Extensions.Get(extensionPoint, memberNames); - } - - public static T Get(this IReflectionExtensionPoint extensionPoint, IEnumerable memberNames) - { - return Extensions.Get(extensionPoint, memberNames); - } - - public static T Get(this IReflectionExtensionPoint extensionPoint, IEnumerable memberNames) - { - return Extensions.Get(extensionPoint, memberNames); - } - - public static object Get(this IReflectionExtensionPoint extensionPoint, - IEnumerable descriptors) - { - return Extensions.Get(extensionPoint, descriptors); - } - - public static T Get(this IReflectionExtensionPoint extensionPoint, - IEnumerable descriptors) - { - return Extensions.Get(extensionPoint, descriptors); - } - - public static T Get(this IReflectionExtensionPoint extensionPoint, - IEnumerable descriptors) - { - return Extensions.Get(extensionPoint, descriptors); - } - - public static void Set(this IReflectionExtensionPoint extensionPoint, string memberName, T value) - { - Extensions.Set(extensionPoint, memberName, value); - } - - public static void Set(this IReflectionExtensionPoint extensionPoint, IEnumerable memberNames, - T value) - { - Extensions.Set(extensionPoint, memberNames, value); - } - - public static void Set(this IReflectionExtensionPoint extensionPoint, IEnumerable memberNames, - T value) - { - Extensions.Set(extensionPoint, memberNames, value); - } - - public static void Set(this IReflectionExtensionPoint extensionPoint, - IEnumerable descriptors, T value) - { - Extensions.Set(extensionPoint, descriptors, value); - } - - public static void Set(this IReflectionExtensionPoint extensionPoint, - IEnumerable descriptors, T value) - { - Extensions.Set(extensionPoint, descriptors, value); - } - - public static IEnumerable GetValueDescriptors( - this IReflectionExtensionPoint extensionPoint, IEnumerable memberNames) - { - return Extensions.GetValueDescriptors(extensionPoint, memberNames); - } - - public static IEnumerable GetDescriptors(this IReflectionExtensionPoint extensionPoint, - IEnumerable memberNames) - { - return Extensions.GetDescriptors(extensionPoint, memberNames); - } - - public static IMemberDescriptor GetDescriptor(this IReflectionExtensionPoint extensionPoint) - { - return Extensions.GetDescriptor(extensionPoint); - } - - public static IMemberDescriptor GetDescriptor(this IMemberDescriptor descriptor, string memberName) - { - return Extensions.GetDescriptor(descriptor, memberName); - } - - public static IMemberDescriptor FindDescriptor(this IMemberDescriptor descriptor, string memberName) - { - return Extensions.FindDescriptor(descriptor, memberName); - } - - public static IValueMemberDescriptor GetValueDescriptor(this IReflectionExtensionPoint extensionPoint, - string memberName) - { - return Extensions.GetValueDescriptor(extensionPoint, memberName); - } - - public static IValueMemberDescriptor FindValueDescriptor(this IReflectionExtensionPoint extensionPoint, - string memberName) - { - return Extensions.FindValueDescriptor(extensionPoint, memberName); - } - - public static IMemberDescriptor GetDescriptor(this IReflectionExtensionPoint extensionPoint, string memberName) - { - return Extensions.GetDescriptor(extensionPoint, memberName); - } - - public static IMemberDescriptor GetDescriptor(this IReflectionExtensionPoint extensionPoint, Expression> func) - { - return Extensions.GetDescriptor(extensionPoint, func); - } - - public static IMemberDescriptor GetDescriptor(this IReflectionExtensionPoint extensionPoint, Expression> func) - { - return Extensions.GetDescriptor(extensionPoint, func); - } - - public static IMemberDescriptor FindDescriptor(this IReflectionExtensionPoint extensionPoint, string memberName) - { - return Extensions.FindDescriptor(extensionPoint, memberName); - } - - public static IMemberDescriptor FindDescriptor(this IReflectionExtensionPoint extensionPoint, string memberName, - BindingContract contract) - { - return Extensions.FindDescriptor(extensionPoint, memberName, contract); - } - - public static IReflectionExtensionPoint Reflection(this Type type) - { - return Extensions.Reflection(type); - } - - public static IReflectionExtensionPoint Reflection(this T instance) - { - return Extensions.Reflection(instance); - } - - public static IMemberDescriptor GetDescriptor(this MemberInfo mi) - { - return Extensions.GetDescriptor(mi); - } - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/TypeDescriptor.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/TypeDescriptor.cs deleted file mode 100644 index 5e89885e8a0e..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/TypeDescriptor.cs +++ /dev/null @@ -1,68 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -#if !WINDOWS_UWP -using System; - -namespace Uno.Reflection -{ - internal class TypeDescriptor : MemberDescriptor - { - public TypeDescriptor(Type type) - : base(type) - { - } - - public override Type Type - { - get { return MemberInfo; } - } - - public override bool IsStatic - { - get { return MemberInfo.IsAbstract && MemberInfo.IsSealed; } - } - - public override bool IsGeneric - { - get { return MemberInfo.IsGenericType; } - } - - public override bool IsOpen - { - get { return MemberInfo.IsGenericTypeDefinition; } - } - - public override IMemberDescriptor Open() - { - return IsClosed ? new TypeDescriptor(MemberInfo.GetGenericTypeDefinition()) : base.Open(); - } - - // TODO: params are ignored - public override IMemberDescriptor Close(params Type[] types) - { - if (!IsOpen) - { - return base.Close(types); - } - - var closedType = MemberInfo.MakeGenericType(types); - - return new TypeDescriptor(closedType); - } - } -} -#endif diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/TypeExtensions.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/TypeExtensions.cs index f8917db0ee74..5d2e1baecdc4 100644 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/TypeExtensions.cs +++ b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/TypeExtensions.cs @@ -18,7 +18,6 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using Uno.Reflection; namespace Uno.Extensions { @@ -41,141 +40,6 @@ public static IEnumerable GetConstructors(this TypeInfo type) } #endif - public static MemberInfo FindMemberInfo(this Type type, string memberName, BindingContract contract) - { - var mi = FindInheritedMember(type, memberName, contract); - - if (mi == null && - (contract.Behavior & BindingBehavior.Interface) == BindingBehavior.Interface) - { - foreach (var interfaceType in type.GetInterfaces()) - { - mi = FindInheritedMember(interfaceType, memberName, contract); - - if (mi != null) - { - break; - } - } - } - - return mi; - } - - public static MemberInfo FindInheritedMember(Type type, string memberName, BindingContract contract) - { - MemberInfo mi = null; - - while (type != null) - { - mi = FindMember(type, memberName, contract); - - if (mi != null || - (contract.Behavior & BindingBehavior.Inherited) != BindingBehavior.Inherited) - { - break; - } - -#if !WINDOWS_UWP && !HAS_CRIPPLEDREFLECTION - type = type.BaseType; -#else - type = type.GetTypeInfo().BaseType; -#endif - } - - return mi; - } - - public static MemberInfo FindMember(Type type, string memberName, BindingContract contract) - { - MemberInfo memberInfo; - - if ((contract.MemberType & MemberTypes.Property) == MemberTypes.Property) - { -#if !WINDOWS_UWP && !HAS_CRIPPLEDREFLECTION - memberInfo = type.GetProperty(memberName, contract.BindingFlags, null, contract.ReturnType, - contract.SafeTypes, null); -#elif HAS_CRIPPLEDREFLECTION && !HAS_TYPEINFO - memberInfo = type.GetProperty(memberName, contract.ReturnType, contract.SafeTypes); -#else - memberInfo = type.GetTypeInfo().GetDeclaredProperty(memberName); -#endif - - if (memberInfo != null) - { - return memberInfo; - } - } - - if ((contract.MemberType & MemberTypes.Event) == MemberTypes.Event) - { -#if !WINDOWS_UWP && !HAS_CRIPPLEDREFLECTION - memberInfo = type.GetEvent(memberName, contract.BindingFlags); -#else - memberInfo = type.GetTypeInfo().GetDeclaredEvent(memberName); -#endif - - if (memberInfo != null) - { - return memberInfo; - } - } - - if ((contract.MemberType & MemberTypes.Field) == MemberTypes.Field) - { -#if !WINDOWS_UWP && !HAS_CRIPPLEDREFLECTION - memberInfo = type.GetField(memberName, contract.BindingFlags); -#else - memberInfo = type.GetTypeInfo().GetDeclaredField(memberName); -#endif - - if (memberInfo != null) - { - return memberInfo; - } - } - - if ((contract.MemberType & MemberTypes.Method) == MemberTypes.Method) - { -#if !WINDOWS_UWP && !HAS_CRIPPLEDREFLECTION - memberInfo = contract.Types == null - ? type.GetMethod(memberName, contract.BindingFlags) - : type.GetMethod(memberName, contract.BindingFlags, null, contract.Types, null); -#elif HAS_CRIPPLEDREFLECTION && !HAS_TYPEINFO - memberInfo = contract.Types == null - ? type.GetMethod(memberName, contract.BindingFlags) - : type.GetMethod(memberName, contract.Types); - -#else - memberInfo = type.GetTypeInfo() - .GetDeclaredMethods(memberName) - .FirstOrDefault( - m => m.GetParameters().Select(p => p.ParameterType).Equality().Equal(contract.Types) - ); -#endif - - if (memberInfo != null) - { - return memberInfo; - } - } - - if ((contract.MemberType & MemberTypes.NestedType) == MemberTypes.NestedType) - { -#if !WINDOWS_UWP - memberInfo = type.GetNestedType(memberName, contract.BindingFlags); -#else - memberInfo = type.GetTypeInfo().GetDeclaredNestedType(memberName); -#endif - if (memberInfo != null) - { - return memberInfo; - } - } - - return null; - } - #if !WINDOWS_UWP /// /// Gets whether null can be assigned to a variable of the given diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/ValueMemberDescriptor.cs b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/ValueMemberDescriptor.cs deleted file mode 100644 index 0a1ff5d6fdb5..000000000000 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.Compatibility/Reflection/ValueMemberDescriptor.cs +++ /dev/null @@ -1,64 +0,0 @@ -// ****************************************************************** -// Copyright � 2015-2018 Uno Platform Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// ****************************************************************** -using System; -using System.Reflection; - -namespace Uno.Reflection -{ - internal abstract class ValueMemberDescriptor : MemberDescriptor, IValueMemberDescriptor - where TMemberInfo : MemberInfo - { - public ValueMemberDescriptor(TMemberInfo memberInfo) - : base(memberInfo) - { - } - - #region IValueMemberDescriptor Members - - public virtual object GetValue(object instance) - { - throw new NotImplementedException(); - } - - public virtual void SetValue(object instance, object value) - { - throw new NotImplementedException(); - } - - public virtual Action ToCompiledSetValue() - { - throw new NotImplementedException(); - } - - public virtual Action ToCompiledSetValue(bool strict) - { - throw new NotImplementedException(); - } - - public virtual Func ToCompiledGetValue() - { - throw new NotImplementedException(); - } - - public virtual Func ToCompiledGetValue(bool strict) - { - throw new NotImplementedException(); - } - - #endregion - } -} diff --git a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.props b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.props index 7f2eb4add5a5..eefffbbf0105 100644 --- a/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.props +++ b/src/Uno.Foundation/Uno.Core.Extensions/Uno.Core.Extensions.props @@ -16,15 +16,6 @@ - - - - - - - - - @@ -38,28 +29,7 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/src/Uno.UI.Tests/BinderTests/Given_Binder.FastConvert.cs b/src/Uno.UI.Tests/BinderTests/Given_Binder.FastConvert.cs index f371f573b236..7c4bd38a3b80 100644 --- a/src/Uno.UI.Tests/BinderTests/Given_Binder.FastConvert.cs +++ b/src/Uno.UI.Tests/BinderTests/Given_Binder.FastConvert.cs @@ -16,7 +16,6 @@ using Uno.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Media; -using Uno.Conversion; using Windows.UI.Text; using Windows.UI.Xaml.Controls; using System.Reflection; diff --git a/src/Uno.UI.Tests/BinderTests/Given_Binder.GeneratedAttached.cs b/src/Uno.UI.Tests/BinderTests/Given_Binder.GeneratedAttached.cs index 809865318835..c3ab7e1f8e08 100644 --- a/src/Uno.UI.Tests/BinderTests/Given_Binder.GeneratedAttached.cs +++ b/src/Uno.UI.Tests/BinderTests/Given_Binder.GeneratedAttached.cs @@ -16,7 +16,6 @@ using Uno.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Media; -using Uno.Conversion; using Microsoft.Extensions.Logging; using Windows.UI.Xaml.Controls; using System.Threading; diff --git a/src/Uno.UI.Tests/BinderTests/Given_Binder.LocalCache.cs b/src/Uno.UI.Tests/BinderTests/Given_Binder.LocalCache.cs index 706c873329e5..656940669baf 100644 --- a/src/Uno.UI.Tests/BinderTests/Given_Binder.LocalCache.cs +++ b/src/Uno.UI.Tests/BinderTests/Given_Binder.LocalCache.cs @@ -16,7 +16,6 @@ using Uno.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Media; -using Uno.Conversion; using Microsoft.Extensions.Logging; using Windows.UI.Xaml.Controls; using System.Threading; diff --git a/src/Uno.UI.Tests/BinderTests/Given_Binder.cs b/src/Uno.UI.Tests/BinderTests/Given_Binder.cs index cec8050d70e6..43ed5e84f519 100644 --- a/src/Uno.UI.Tests/BinderTests/Given_Binder.cs +++ b/src/Uno.UI.Tests/BinderTests/Given_Binder.cs @@ -16,7 +16,6 @@ using Uno.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Media; -using Uno.Conversion; using Microsoft.Extensions.Logging; using Windows.UI.Xaml.Controls; using System.Threading; diff --git a/src/Uno.UI.Tests/BinderTests/Given_Binder.inpc.cs b/src/Uno.UI.Tests/BinderTests/Given_Binder.inpc.cs index abbcf1c53442..62294872a095 100644 --- a/src/Uno.UI.Tests/BinderTests/Given_Binder.inpc.cs +++ b/src/Uno.UI.Tests/BinderTests/Given_Binder.inpc.cs @@ -15,7 +15,6 @@ using Uno.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Media; -using Uno.Conversion; using Microsoft.Extensions.Logging; using Windows.UI.Xaml.Controls; using System.Threading; diff --git a/src/Uno.UI/DataBinding/BindingPropertyHelper.FastConvert.cs b/src/Uno.UI/DataBinding/BindingPropertyHelper.FastConvert.cs index 32d3cc73f00d..8aee30fa21a6 100644 --- a/src/Uno.UI/DataBinding/BindingPropertyHelper.FastConvert.cs +++ b/src/Uno.UI/DataBinding/BindingPropertyHelper.FastConvert.cs @@ -343,6 +343,11 @@ private static bool FastStringConvert(Type outputType, string input, ref object return true; } + if (FastStringToBoolean(outputType, input, ref output)) + { + return true; + } + // Fallback for Enums. Leave it at the end. if (outputType.IsEnum) { @@ -973,6 +978,17 @@ private static bool FastStringToTypeConvert(Type outputType, string input, ref o } } + private static bool FastStringToBoolean(Type outputType, string input, ref object output) + { + if (outputType == typeof(bool) && bool.TryParse(input, out var result)) + { + output = result; + return true; + } + + return false; + } + private static List GetDoubleValues(string input) { var list = new List(); diff --git a/src/Uno.UI/DataBinding/BindingPropertyHelper.cs b/src/Uno.UI/DataBinding/BindingPropertyHelper.cs index e2beb8f94332..6329fe3c8422 100644 --- a/src/Uno.UI/DataBinding/BindingPropertyHelper.cs +++ b/src/Uno.UI/DataBinding/BindingPropertyHelper.cs @@ -14,12 +14,12 @@ using Uno; using Windows.UI.Xaml; using System.Collections; -using Uno.Conversion; using Windows.UI.Xaml.Data; using System.Dynamic; using System.Runtime.CompilerServices; using System.Diagnostics.CodeAnalysis; +using System.ComponentModel; namespace Uno.UI.DataBinding { @@ -62,7 +62,6 @@ internal static partial class BindingPropertyHelper static BindingPropertyHelper() { MethodInvokerBuilder = DefaultInvokerBuilder; - Conversion = new DefaultConversionExtensions(); } internal static void ClearCaches() @@ -92,8 +91,6 @@ internal static void ClearCaches() /// public static IBindableMetadataProvider? BindableMetadataProvider { get; set; } - public static DefaultConversionExtensions Conversion { get; private set; } - public static bool IsEvent(Type type, string property) { var key = CachedTuple.Create(type, property); @@ -1230,38 +1227,73 @@ private static DependencyProperty FindDependencyProperty(Type ownerType, string .FirstOrDefault(); } - internal static object? Convert(Func? propertyType, object? value) + private static object? ConvertToEnum(Type enumType, object value) { - if (value != null && propertyType != null) + var valueString = value.ToString(); + + FieldInfo? defaultValue = null; + foreach (var field in enumType.GetFields()) { - var t = propertyType(); + if (field.Name.Equals(valueString, StringComparison.OrdinalIgnoreCase)) + { + return Enum.Parse(enumType, field.Name); + } - if (!value.GetType().Is(t)) + var descriptionAttribute = field.GetCustomAttributes().FirstOrDefault(); + if (descriptionAttribute is not null) { - if (FastConvert(t, value, out var fastConvertResult)) + if (descriptionAttribute.Description.Equals(valueString, StringComparison.CurrentCultureIgnoreCase) || + descriptionAttribute.Description.Equals(valueString, StringComparison.InvariantCultureIgnoreCase)) { - return fastConvertResult; +#if NET7_0_OR_GREATER + if (Enum.TryParse(enumType, field.Name, ignoreCase: true, out var enumValue)) + { + return enumValue; + } +#else + try + { + return Enum.Parse(enumType, field.Name, ignoreCase: true); + } + catch (Exception) + { + } +#endif } - else if (t != typeof(object)) + + if (descriptionAttribute.Description == "?") { - value = ConvertWithConvertionExtension(value, t); + defaultValue = field; } } } - return value; + + if (defaultValue is not null) + { +#if NET7_0_OR_GREATER + if (Enum.TryParse(enumType, defaultValue.Name, ignoreCase: true, out var enumValue)) + { + return enumValue; + } +#else + try + { + return Enum.Parse(enumType, defaultValue.Name, ignoreCase: true); + } + catch (Exception) + { + } +#endif + } + + return DependencyProperty.UnsetValue; } - /// - /// This method contains or is called by a try/catch containing method and - /// can be significantly slower than other methods as a result on WebAssembly. - /// See https://github.com/dotnet/runtime/issues/56309 - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static object ConvertWithConvertionExtension(object? value, Type? t) + private static object? ConvertPrimitiveToPrimitive(Type type, object value) { try { - value = Conversion.To(value, t, CultureInfo.CurrentCulture); + return System.Convert.ChangeType(value, type, CultureInfo.CurrentCulture); } catch (Exception) { @@ -1269,12 +1301,113 @@ private static object ConvertWithConvertionExtension(object? value, Type? t) // The problem is that we don't actually know which culture we must use in advance. // Values can come from the xaml (invariant culture) or from a two way binding (current culture). // The real solution would be to pass a culture or source when setting a value in a Dependency Property. - value = Conversion.To(value, t, CultureInfo.InvariantCulture); + return System.Convert.ChangeType(value, type, CultureInfo.InvariantCulture); + } + } + + private static object? ConvertUsingTypeDescriptor(Type type, object value) + { + var valueTypeConverter = TypeDescriptor.GetConverter(value.GetType()); + if (valueTypeConverter.CanConvertTo(type)) + { + try + { + return valueTypeConverter.ConvertTo(null, CultureInfo.CurrentCulture, value, type); + } + catch (Exception) + { + return valueTypeConverter.ConvertTo(null, CultureInfo.InvariantCulture, value, type); + } + } + + if (type == typeof(float)) + { + var valueToString = value.ToString(); + if (float.TryParse(valueToString, NumberStyles.Float, CultureInfo.CurrentCulture, out var fValue)) + { + return fValue; + } + + if (float.TryParse(valueToString, NumberStyles.Float, CultureInfo.InvariantCulture, out fValue)) + { + return fValue; + } + } + else if (type == typeof(decimal)) + { + var valueToString = value.ToString(); + if (decimal.TryParse(valueToString, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out var dValue)) + { + return dValue; + } + if (decimal.TryParse(valueToString, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out dValue)) + { + return dValue; + } + } + + try + { + return TypeDescriptor.GetConverter(type).ConvertFrom(null, CultureInfo.CurrentCulture, value); + } + catch (Exception) + { + return TypeDescriptor.GetConverter(type).ConvertFrom(null, CultureInfo.InvariantCulture, value); + } + } + + internal static object? Convert(Func? propertyType, object? value) + { + if (value != null && propertyType != null) + { + var t = propertyType(); + + if (!value.GetType().Is(t)) + { + if (FastConvert(t, value, out var fastConvertResult)) + { + return fastConvertResult; + } + else if (t is null || t == typeof(object)) + { + return value; + } + else if (t == typeof(string)) + { + return value?.ToString(); + } + else if (t.IsEnum) + { + return ConvertToEnum(t, value); + } + else if ((Nullable.GetUnderlyingType(t) ?? t) is { IsPrimitive: true } toTypeUnwrapped && IsPrimitive(value)) + { + return ConvertPrimitiveToPrimitive(toTypeUnwrapped, value); + } + else + { + return ConvertUsingTypeDescriptor(t, value); + } + } } return value; } + private static bool IsPrimitive(object value) + { + return value.GetType().IsPrimitive + || value is string +#if __IOS__ + // Those are platform primitives provided for 64 bits compatibility + // with iOS 8.0 and later + || value is nfloat + || value is nint + || value is nuint +#endif + ; + } + private static object UnsetValueGetter(object unused) => DependencyProperty.UnsetValue;