Exemplo n.º 1
0
        /// <summary>
        /// Find the converter functor from source to target
        /// </summary>
        /// <param name="source">Dimension to convert from</param>
        /// <param name="target">Dimension to concert to</param>
        /// <param name="power"></param>
        /// <returns>A functor to convert from source into target</returns>
        public static Func <double, double> From(IDimension source, IDistance target, int power)
        {
            var dispatcher = new VisitorDispatcher("Convert");

            var result = dispatcher.Accept(source.GetType(), new DistanceConverter(), source, target, power);

            return((Func <double, double>)result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Find the converter functor from source to target
        /// </summary>
        /// <param name="source">Dimension to convert from</param>
        /// <param name="target">Dimension to concert to</param>
        /// <param name="power"></param>
        /// <returns>A functor to convert from source into target</returns>
        public static Func<double, double> From(IDimension source, IDistance target, int power)
        {
            var dispatcher = new VisitorDispatcher("Convert");

            var result = dispatcher.Accept(source.GetType(), new DistanceConverter(), source, target, power);

            return (Func<double, double>)result;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Convert a measure unit to Metric
        /// </summary>
        /// <typeparam name="TDistance"> Type of the measure </typeparam>
        /// <param name="unit"> Instance of the measure </param>
        /// <param name="dimension"> New Dimension to convert </param>
        /// <returns> A new measure with the conversion </returns>
        public static IUnit <IDistance> In <TDistance>(this IUnit <TDistance> unit, IDistance dimension)
            where TDistance : IDistance
        {
            var dispatcher = new VisitorDispatcher();

            var result = (IUnit <IDistance>)dispatcher.Accept(unit.Dimension.GetType(),
                                                              new DistanceConverterVisitor(),
                                                              unit.Quantity,
                                                              unit.Dimension,
                                                              dimension);

            if (result == null)
            {
                var message = string.Format("I don't know how to convert {0} to metric", unit.Dimension.GetType());

                throw new InvalidOperationException(message);
            }

            return(result);
        }