/// <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); }
/// <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; }
/// <summary> /// Non generic version that calls the dispatcher /// </summary> /// <param name="rtype">Type of the value to generate</param> /// <returns>A new random value of type rtype</returns> public object Generate(Type rtype) { if (rtype.IsEnum) { var values = Enum.GetValues(rtype); var index = random.Next(0, values.Length - 1); return(values.GetValue(index)); } return(dispatcher.Accept(rtype, this, rtype)); }
/// <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); }