예제 #1
0
        /// <summary>
        /// Allows for conversion of <see cref="IUnit"/> derived type.
        /// </summary>
        /// <remarks>
        /// Note: the type <typeparamref name="TN1"/> must be a relative of <typeparamref name="T1"/>.
        /// Note: the type <typeparamref name="TN2"/> must be a relative of <typeparamref name="T2"/>.
        /// </remarks>
        /// <typeparam name="TN1">The new type you want to convert the current scalar's type to.</typeparam>
        /// <typeparam name="TN2">The new type you want to convert the current scalar's type to.</typeparam>
        /// <returns>A new scalar with type of <typeparamref name="TN1"/>, <typeparamref name="TN2"/> who's value has been converted to <typeparamref name="TN1"/>, <typeparamref name="TN2"/>.</returns>
        public Scalar <TN1, TN2> To <TN1, TN2>()
            where TN1 : class, IUnit, new()
            where TN2 : class, IUnit, new()
        {
            TN1 newUnit1 = new TN1();
            TN2 newUnit2 = new TN2();

            foreach (Type t in Unit1.GetType().GetInterfaces())
            {
                if (newUnit1.GetType().GetInterface(t.Name) == null)
                {
                    throw new InvalidCastException(String.Format(CultureInfo.CurrentCulture, "You cannot convert {0} to {1}", Unit1.GetType(), newUnit1.GetType()));
                }
            }
            foreach (Type t in Unit2.GetType().GetInterfaces())
            {
                if (newUnit2.GetType().GetInterface(t.Name) == null)
                {
                    throw new InvalidCastException(String.Format(CultureInfo.CurrentCulture, "You cannot convert {0} to {1}", Unit2.GetType(), newUnit2.GetType()));
                }
            }

            double firstUnitValue = ConvertUnits(newUnit1.GetType(), Unit1.GetType(), Value, Unit1Power);
            double newValue       = ConvertUnits(newUnit2.GetType(), Unit2.GetType(), firstUnitValue, Unit2Power);

            return(new Scalar <TN1, TN2>()
            {
                Value = newValue,
                Unit1 = newUnit1,
                Unit2 = newUnit2,
                Unit1Power = Unit1Power,
                Unit2Power = Unit2Power,
            });
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            // number
            StringBuilder returnString = new StringBuilder(Value.ToString(CultureInfo.CurrentCulture));

            // first unit
            returnString.AppendFormat(
                " {0}{1}",
                Unit1.GetType().Name,
                ((Unit1Power == 1) ?
                 String.Empty :
                 ("^" + Unit1Power)));

            // divider
            returnString.Append((Unit2Power < 0) ? "/" : "*");

            //second unit
            returnString.AppendFormat(
                "{0}{1}",
                Unit2.GetType().Name,
                ((Math.Abs(Unit2Power) == 1) ?
                 String.Empty :
                 ("^" + Math.Abs(Unit2Power))));

            return(returnString.ToString());
        }