예제 #1
0
        /// <summary>
        /// Tries to convert convert the value from the source type to the result type.
        /// </summary>
        /// <param name="conversionPair">The conversion pair representing the source and result types to try converting.</param>
        /// <param name="value">The value to convert.</param>
        /// <param name="result">The result if the conversion was successful; otherwise null.</param>
        /// <param name="exception">The exception if one was thrown during the conversion; otherwise null.</param>
        /// <returns>An enumeration value indicating the outcome of the conversion attempt.</returns>
        public ConversionStatus TryConvert(ConversionPair conversionPair, object value, out object result, out Exception exception)
        {
            Contracts.Requires.That(conversionPair != null);
            Contracts.Requires.That(value != null ? value.GetType() == conversionPair.SourceType : true);

            DefaultDictionary <object, object> valuePool;

            if (this.valuePools.TryGetValue(conversionPair, out valuePool))
            {
                try
                {
                    result    = valuePool[value];
                    exception = null;
                    return(ConversionStatus.Success);
                }
                catch (Exception conversionException)
                {
                    result    = null;
                    exception = conversionException;
                    return(ConversionStatus.Exception);
                }
            }
            else
            {
                result    = null;
                exception = null;
                return(ConversionStatus.NoConverterFound);
            }
        }
예제 #2
0
        /// <summary>
        /// Tries to convert convert the value from the source type to the result type.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="value">The value to convert.</param>
        /// <param name="result">The result if the conversion was successful; otherwise the default of the result type.</param>
        /// <param name="exception">The exception if one was thrown during the conversion; otherwise null.</param>
        /// <returns>An enumeration value indicating the outcome of the conversion attempt.</returns>
        public ConversionStatus TryConvert <TSource, TResult>(TSource value, out TResult result, out Exception exception)
        {
            DefaultDictionary <object, object> valuePool;

            if (this.valuePools.TryGetValue(ConversionPair.CreateNew <TSource, TResult>(), out valuePool))
            {
                try
                {
                    result    = (TResult)valuePool[value];
                    exception = null;
                    return(ConversionStatus.Success);
                }
                catch (Exception conversionException)
                {
                    result    = default(TResult);
                    exception = conversionException;
                    return(ConversionStatus.Exception);
                }
            }
            else
            {
                result    = default(TResult);
                exception = null;
                return(ConversionStatus.NoConverterFound);
            }
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NonTypedConverter"/> class.
        /// </summary>
        /// <param name="converter">The converter to wrap.</param>
        public NonTypedConverter(object converter)
        {
            Contracts.Requires.That(converter != null);
            Contracts.Requires.That(IsTypeConverter(converter));

            Type sourceType = converter.GetType().GetGenericArguments()[0];
            Type resultType = converter.GetType().GetGenericArguments()[1];

            this.ConversionPair = ConversionPair.CreateNew(sourceType, resultType);
            this.converter      = converter;
        }