public void Dynamic_Happy()
        {
            Assert.AreEqual(42, _converter.Convert <object>(42).To <int>(), "DynamicConverter should typecheck int and use identity converter");
            Assert.AreEqual(42m, _converter.Convert <object>(42m).To <decimal>(), "DynamicConverter should typecheck decimal and use identity converter");
            var res = _converter.DoConversion <object, int>(0);

            Assert.IsTrue(res.IsSuccessful, "DynamicConverter should typecheck int and use identity converter (default value, successful)");
            Assert.AreEqual(0, res.Result, "DynamicConverter should typecheck int and use identity converter (default value, value equal)");
        }
Пример #2
0
        /// <summary>
        /// This method performs a conversion and exposes it as a 'Try' method.
        /// </summary>
        /// <typeparam name="T">The type to be converted from.</typeparam>
        /// <typeparam name="U">The type to be converted to.</typeparam>
        /// <param name="converter">The IDataConverter object.</param>
        /// <param name="value">The value to be converted.</param>
        /// <param name="result">Out result parameter for the conversion result.</param>
        /// <returns>True if the conversion was successful.</returns>
        public static bool TryConvert <T, U>(this IDataConverter converter, T value, out U result)
        {
            var conversionResult = converter.DoConversion <T, U>(value);

            result = conversionResult.Result;
            return(conversionResult.IsSuccessful);
        }
Пример #3
0
 /// <summary>
 /// Converts the value to type U, or a default value if conversion fails.
 /// </summary>
 /// <typeparam name="U">The type to be converted to.</typeparam>
 /// <param name="default">A default value to return in case of failure.</param>
 /// <returns>The conversion result, or some default value.</returns>
 public U To <U>(U @default = default(U))
 => _converter.DoConversion <T, U>(_value).GetOrDefault(@default);