Exemple #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 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");
        }
        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");
        }
        /// <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 + "] to target type [" + targetType.Name + "]",
                      "sourceInstance");
        }