Exemplo n.º 1
0
        /// <summary>
        /// Extends ConvertTo so that methods that return a specific type object given a Type parameter can be
        /// used as generic method and casting is not required.
        /// <example>
        /// typeconverter.ConvertTo<int>(value);
        /// </example>
        /// </summary>
        public static T ConvertTo <T>(this DecimalConverter typeconverter, Object value)
        {
            if (typeconverter == null)
            {
                throw new ArgumentNullException("typeconverter");
            }

            return((T)typeconverter.ConvertTo(value, typeof(T)));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Extends ConvertTo so that methods that return a specific type object given a Type parameter can be
        /// used as generic method and casting is not required.
        /// <example>
        /// decimalconverter.ConvertTo<int>(context, culture, value);
        /// </example>
        /// </summary>
        public static T ConvertTo <T>(this DecimalConverter decimalconverter, ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value)
        {
            if (decimalconverter == null)
            {
                throw new ArgumentNullException("decimalconverter");
            }

            return((T)decimalconverter.ConvertTo(context, culture, value, typeof(T)));
        }
Exemplo n.º 3
0
        public static void ConvertToInt32()
        {
            // Arrange
            var converter = new DecimalConverter();

            // Act
            var result = converter.ConvertTo(100M, typeof(int));

            // Assert
            Assert.Equal(100, result);
        }
Exemplo n.º 4
0
        public override object ConvertTo(object sourceValue, Type destinationType, IFormatProvider formatProvider, bool ignoreCase)
        {
            if (sourceValue == null)
            {
                throw new NotSupportedException();
            }

            if (sourceValue is Money moneyValue)
            {
                if (destinationType == typeof(decimal))
                {
                    return(moneyValue.Value);
                }
                else
                {
                    DecimalConverter dc = new DecimalConverter();
                    return(dc.ConvertTo(moneyValue.Value, destinationType));
                }
            }

            return(new NotSupportedException());
        }
Exemplo n.º 5
0
 /// <summary>
 /// Convert a Decimal value into a string representation.
 /// </summary>
 /// <param name="value">Decimal value to be converted.</param>
 /// <returns>String representation of the Decimal instance.</returns>
 public static string DecimalToString(Decimal value)
 {
     return((string)_dc.ConvertTo(null, CultureInfo.InvariantCulture, value, _stringType));
 }