Exemplo n.º 1
0
        /// <summary>
        /// Converts an object to the target type.
        /// </summary>
        /// <param name="sourceInstance">The object to convert to the target type.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <returns>The converted object.</returns>
        /// <remarks>
        /// <para>
        /// Converts an object to the target type.
        /// </para>
        /// </remarks>
        public static object ConvertTypeTo(object sourceInstance, Type targetType)
        {
            Type sourceType = sourceInstance.GetType();

            // Check if we can assign directly from the source type to the target type
            if (targetType.IsAssignableFrom(sourceType))
            {
                return(sourceInstance);
            }

            // Look for a TO converter
            IConvertTo tcSource = ConverterRegistry.GetConvertTo(sourceType, targetType);

            if (tcSource != null)
            {
                if (tcSource.CanConvertTo(targetType))
                {
                    return(tcSource.ConvertTo(sourceInstance, targetType));
                }
            }

            // Look for a FROM converter
            IConvertFrom tcTarget = ConverterRegistry.GetConvertFrom(targetType);

            if (tcTarget != null)
            {
                if (tcTarget.CanConvertFrom(sourceType))
                {
                    return(tcTarget.ConvertFrom(sourceInstance));
                }
            }

            throw new ArgumentException("Cannot convert source object [" + sourceInstance.ToString() + "] to target type [" + targetType.Name + "]", "sourceInstance");
        }
Exemplo n.º 2
0
        public static bool CanConvertTypeTo(Type sourceType, Type targetType)
        {
            if ((sourceType == null) || (targetType == null))
            {
                return(false);
            }
            if (targetType.IsAssignableFrom(sourceType))
            {
                return(true);
            }
            IConvertTo convertTo = ConverterRegistry.GetConvertTo(sourceType, targetType);

            if ((convertTo != null) && convertTo.CanConvertTo(targetType))
            {
                return(true);
            }
            IConvertFrom convertFrom = ConverterRegistry.GetConvertFrom(targetType);

            return((convertFrom != null) && convertFrom.CanConvertFrom(sourceType));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts an object to the target type.
        /// </summary>
        /// <param name="sourceInstance">The object to convert to the target type.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <returns>The converted object.</returns>
        /// <remarks>
        /// <para>
        /// Converts an object to the target type.
        /// </para>
        /// </remarks>
        public static object ConvertTypeTo(object sourceInstance, Type targetType)
        {
            Type type = sourceInstance.GetType();

            if (CompatibilityExtensions.IsAssignableFrom(targetType, type))
            {
                return(sourceInstance);
            }
            IConvertTo convertTo = ConverterRegistry.GetConvertTo(type, targetType);

            if (convertTo != null && convertTo.CanConvertTo(targetType))
            {
                return(convertTo.ConvertTo(sourceInstance, targetType));
            }
            IConvertFrom convertFrom = ConverterRegistry.GetConvertFrom(targetType);

            if (convertFrom != null && convertFrom.CanConvertFrom(type))
            {
                return(convertFrom.ConvertFrom(sourceInstance));
            }
            throw new ArgumentException("Cannot convert source object [" + sourceInstance.ToString() + "] to target type [" + targetType.Name + "]", "sourceInstance");
        }
Exemplo n.º 4
0
        public static object ConvertTypeTo(object sourceInstance, Type targetType)
        {
            Type c = sourceInstance.GetType();

            if (targetType.IsAssignableFrom(c))
            {
                return(sourceInstance);
            }
            IConvertTo convertTo = ConverterRegistry.GetConvertTo(c, targetType);

            if ((convertTo != null) && convertTo.CanConvertTo(targetType))
            {
                return(convertTo.ConvertTo(sourceInstance, targetType));
            }
            IConvertFrom convertFrom = ConverterRegistry.GetConvertFrom(targetType);

            if ((convertFrom != null) && convertFrom.CanConvertFrom(c))
            {
                return(convertFrom.ConvertFrom(sourceInstance));
            }
            string[] textArray1 = new string[] { "Cannot convert source object [", sourceInstance.ToString(), "] to target type [", targetType.Name, "]" };
            throw new ArgumentException(string.Concat(textArray1), "sourceInstance");
        }
Exemplo n.º 5
0
        // /// <summary>
        // /// Looks up the <see cref="IConvertFrom"/> for the target type.
        // /// </summary>
        // /// <param name="target">The type to lookup the converter for.</param>
        // /// <returns>The converter for the specified type.</returns>
        // public static IConvertFrom GetTypeConverter(Type target)
        // {
        // IConvertFrom converter = ConverterRegistry.GetConverter(target);
        // if (converter == null)
        // {
        // throw new InvalidOperationException("No type converter defined for [" + target + "]");
        // }
        // return converter;
        // }

        /// <summary>
        /// Checks if there is an appropriate type conversion from the source type to the target type.
        /// </summary>
        /// <param name="sourceType">The type to convert from.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <returns><c>true</c> if there is a conversion from the source type to the target type.</returns>
        /// <remarks>
        /// Checks if there is an appropriate type conversion from the source type to the target type.
        /// <para>
        /// </para>
        /// </remarks>
        public static bool CanConvertTypeTo(Type sourceType, Type targetType)
        {
            if (sourceType == null || targetType == null)
            {
                return(false);
            }

            // Check if we can assign directly from the source type to the target type
            if (targetType.IsAssignableFrom(sourceType))
            {
                return(true);
            }

            // Look for a To converter
            IConvertTo tcSource = ConverterRegistry.GetConvertTo(sourceType, targetType);

            if (tcSource != null)
            {
                if (tcSource.CanConvertTo(targetType))
                {
                    return(true);
                }
            }

            // Look for a From converter
            IConvertFrom tcTarget = ConverterRegistry.GetConvertFrom(targetType);

            if (tcTarget != null)
            {
                if (tcTarget.CanConvertFrom(sourceType))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Checks if there is an appropriate type conversion from the source type to the target type.
        /// </summary>
        /// <param name="sourceType">The type to convert from.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <returns><c>true</c> if there is a conversion from the source type to the target type.</returns>
        /// <remarks>
        /// Checks if there is an appropriate type conversion from the source type to the target type.
        /// <para>
        /// </para>
        /// </remarks>
        public static bool CanConvertTypeTo(Type sourceType, Type targetType)
        {
            if ((object)sourceType == null || (object)targetType == null)
            {
                return(false);
            }
            if (CompatibilityExtensions.IsAssignableFrom(targetType, sourceType))
            {
                return(true);
            }
            IConvertTo convertTo = ConverterRegistry.GetConvertTo(sourceType, targetType);

            if (convertTo != null && convertTo.CanConvertTo(targetType))
            {
                return(true);
            }
            IConvertFrom convertFrom = ConverterRegistry.GetConvertFrom(targetType);

            if (convertFrom != null && convertFrom.CanConvertFrom(sourceType))
            {
                return(true);
            }
            return(false);
        }