Exemplo n.º 1
0
        public object ConvertBack(
            object targetValue,
            Type sourceType,
            object parameter,
            CultureInfo targetCulture)
        {
            Exception exception;

            return(DefaultDataConverter.TryConvert(targetValue, sourceType, targetCulture, CultureInfo.InvariantCulture, out exception));
        }
Exemplo n.º 2
0
        public object Convert(object sourceValue, Type targetType, object parameter, CultureInfo targetCulture)
        {
            if ((sourceValue == DBNull.Value) || (sourceValue == null))
            {
                return(null);
            }

            Exception exception;

            return(DefaultDataConverter.TryConvert(sourceValue, targetType, CultureInfo.InvariantCulture, targetCulture, out exception));
        }
Exemplo n.º 3
0
        private static bool IsConvertibleToAndFrom(Type sourceType, Type targetType)
        {
            if (sourceType == typeof(DateTime))
            {
                return(targetType == typeof(string));
            }

            if (targetType == typeof(DateTime))
            {
                return(sourceType == typeof(string));
            }

            if (sourceType == typeof(char))
            {
                return(DefaultDataConverter.IsConvertibleFromAndToChar(targetType));
            }

            if (targetType == typeof(char))
            {
                return(DefaultDataConverter.IsConvertibleFromAndToChar(sourceType));
            }

            bool sourceTypeFound = false;
            bool targetTypeFound = false;

            for (int i = 0; i < ConvertibleTypes.Length; i++)
            {
                if ((!sourceTypeFound) && (sourceType == ConvertibleTypes[i]))
                {
                    if (targetTypeFound)
                    {
                        return(true);
                    }

                    sourceTypeFound = true;
                }
                else if ((!targetTypeFound) && (targetType == ConvertibleTypes[i]))
                {
                    if (sourceTypeFound)
                    {
                        return(true);
                    }

                    targetTypeFound = true;
                }
            }

            return(false);
        }
Exemplo n.º 4
0
        internal object TryConvertBack(
            object targetValue,
            Type sourceType,
            CultureInfo sourceCulture,
            CultureInfo targetCulture,
            out Exception exception)
        {
            exception = null;

            if ((targetValue == null) || (targetValue == DBNull.Value) ||
                ((sourceType != typeof(string)) && (string.Empty.Equals(targetValue))))
            {
                if (m_sourceSupportsDBNull)
                {
                    return(DBNull.Value);
                }
                else
                {
                    return(null);
                }
            }

            return(DefaultDataConverter.TryConvert(targetValue, sourceType, targetCulture, sourceCulture, out exception));
        }
Exemplo n.º 5
0
        internal static object TryConvert(
            object sourceValue,
            Type targetType,
            CultureInfo sourceCulture,
            CultureInfo targetCulture,
            out Exception exception)
        {
            if (targetCulture == null)
            {
                targetCulture = CultureInfo.CurrentCulture;
            }

            if (sourceCulture == null)
            {
                sourceCulture = CultureInfo.InvariantCulture;
            }

            exception = null;
            Type valueType = null;

            try
            {
                if (sourceValue != null)
                {
                    valueType = sourceValue.GetType();

                    if (targetType.IsAssignableFrom(valueType))
                    {
                        return(sourceValue);
                    }
                }
                else
                {
                    if (!targetType.IsValueType)
                    {
                        return(sourceValue);
                    }
                }

                if (targetType == typeof(string))
                {
                    return(string.Format(targetCulture, "{0}", sourceValue));
                }

                if (sourceValue != null)
                {
                    bool   targetTypeIsNullable = false;
                    Type   targetConversionType = targetType;
                    object resultValue;

                    if ((targetType.IsGenericType) && (targetType.GetGenericTypeDefinition() == typeof(Nullable <>)))
                    {
                        targetTypeIsNullable = true;
                        targetConversionType = targetType.GetGenericArguments()[0];
                    }

                    if (DefaultDataConverter.IsConvertibleToAndFrom(valueType, targetConversionType))
                    {
                        resultValue = System.Convert.ChangeType(sourceValue, targetConversionType, sourceCulture);

                        if (!targetTypeIsNullable)
                        {
                            return(resultValue);
                        }

                        sourceValue = resultValue;
                    }
                }

                TypeConverter cachedTypeConverter;

                lock ( mg_cachedTypeConverter )
                {
                    cachedTypeConverter = mg_cachedTypeConverter[targetType] as TypeConverter;

                    if (cachedTypeConverter == null)
                    {
                        cachedTypeConverter = TypeDescriptor.GetConverter(targetType);
                        mg_cachedTypeConverter[targetType] = cachedTypeConverter;
                    }
                }

                return(cachedTypeConverter.ConvertFrom(null, sourceCulture, sourceValue));
            }
            catch (Exception localException)
            {
                exception = localException;
                return(DependencyProperty.UnsetValue);
            }
        }