internal bool TryConvert(ref object value)
        {
            if (value == null)
            {
                return(!ReturnTypeInfo.IsValueType || ReturnTypeInfo.IsGenericType && ReturnTypeInfo.GetGenericTypeDefinition() == typeof(Nullable <>));
            }

            Type valueType = value.GetType();
            Type type      = ReturnType;

            // Dont support arbitrary IConvertible by limiting which types can use this
            Type[]        convertableTo;
            TypeConverter typeConverterTo;

            if (SimpleConvertTypes.TryGetValue(valueType, out convertableTo) && Array.IndexOf(convertableTo, type) != -1)
            {
                value = Convert.ChangeType(value, type);
            }
            else if (WellKnownConvertTypes.TryGetValue(type, out typeConverterTo) && typeConverterTo.CanConvertFrom(valueType))
            {
                value = typeConverterTo.ConvertFromInvariantString(value.ToString());
            }
            else if (!ReturnTypeInfo.IsAssignableFrom(valueType.GetTypeInfo()))
            {
                // Is there an implicit cast operator ?
                MethodInfo cast = type.GetRuntimeMethod("op_Implicit", new[] { valueType });
                if (cast != null && cast.ReturnType != type)
                {
                    cast = null;
                }
                if (cast == null)
                {
                    cast = valueType.GetRuntimeMethod("op_Implicit", new[] { valueType });
                }
                if (cast != null && cast.ReturnType != type)
                {
                    cast = null;
                }
                if (cast == null)
                {
                    return(false);
                }

                value = cast.Invoke(null, new[] { value });
            }

            return(true);
        }
Example #2
0
        internal bool TryConvert(ref object value)
        {
            if (value == null)
            {
                return(!ReturnTypeInfo.IsValueType || ReturnTypeInfo.IsGenericType && ReturnTypeInfo.GetGenericTypeDefinition() == typeof(Nullable <>));
            }

            Type valueType = value.GetType();
            Type type      = ReturnType;

            // TODO This is temporary fix before deleting CreateByXaml flag in BindableProperty.
            if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(Tizen.NUI.BaseComponents.Selector <>) && valueType.GetGenericArguments()[0] == ReturnType)
            {
                return(true);
            }

            // Dont support arbitrary IConvertible by limiting which types can use this
            Type[]        convertableTo;
            TypeConverter typeConverterTo;

            if (SimpleConvertTypes.TryGetValue(valueType, out convertableTo) && Array.IndexOf(convertableTo, type) != -1)
            {
                value = Convert.ChangeType(value, type);
            }
            else if (WellKnownConvertTypes.TryGetValue(type, out typeConverterTo) && typeConverterTo.CanConvertFrom(valueType))
            {
                value = typeConverterTo.ConvertFromInvariantString(value.ToString());
            }
            else if (UserCustomConvertTypes.TryGetValue(type, out typeConverterTo) && typeConverterTo.CanConvertFrom(valueType))
            {
                //Modification for NUI XAML : user defined converter for DynamicResource can be added
                value = typeConverterTo.ConvertFromInvariantString(value.ToString());
            }
            else if (!ReturnTypeInfo.IsAssignableFrom(valueType.GetTypeInfo()))
            {
                var cast = type.GetImplicitConversionOperator(fromType: valueType, toType: type)
                           ?? valueType.GetImplicitConversionOperator(fromType: valueType, toType: type);

                if (cast == null)
                {
                    return(false);
                }

                value = cast.Invoke(null, new[] { value });
            }

            return(true);
        }