Exemplo n.º 1
0
        /// <summary>
        /// Converts the given amount to the given unit.
        /// </summary>
        public static Amount ConvertTo(Amount amount, Unit toUnit)
        {
            try
            {
                // Performance optimalization:
                if (Object.ReferenceEquals(amount.Unit, toUnit))
                {
                    return(amount);
                }

                // Perform conversion:
                if (amount.Unit.IsCompatibleTo(toUnit))
                {
                    return(new Amount(amount.Value * amount.Unit.Factor / toUnit.Factor, toUnit));
                }
                else
                {
                    UnitConversionKeySlot expectedSlot = new UnitConversionKeySlot(amount.Unit, toUnit);
                    return(Instance._conversions[expectedSlot].Convert(amount).ConvertedTo(toUnit));
                }
            }
            catch (KeyNotFoundException)
            {
                throw new UnitConversionException(amount.Unit, toUnit);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Converts the given amount to the given unit.
 /// </summary>
 public static Amount ConvertTo(Amount amount, Unit toUnit)
 {
     try
     {
         if (amount.Unit.CompatibleTo(toUnit))
         {
             return(new Amount(amount.Value * amount.Unit.Factor / toUnit.Factor, toUnit));
         }
         else
         {
             UnitConversionKeySlot expectedSlot = new UnitConversionKeySlot(amount.Unit, toUnit);
             return(Instance.conversions[expectedSlot].Convert(amount).ConvertedTo(toUnit));
         }
     }
     catch (KeyNotFoundException)
     {
         throw new UnitConversionException(amount.Unit, toUnit);
     }
 }
Exemplo n.º 3
0
            public override bool Equals(object obj)
            {
                UnitConversionKeySlot other = obj as UnitConversionKeySlot;

                return((this.fromType == other.fromType) && (this.toType == other.toType));
            }
Exemplo n.º 4
0
		/// <summary>
		/// Converts the given amount to the given unit.
		/// </summary>
		public static Amount ConvertTo(Amount amount, Unit toUnit)
		{
			try
			{
				if (amount.Unit.CompatibleTo(toUnit))
				{
					return new Amount(amount.Value * amount.Unit.Factor / toUnit.Factor, toUnit);
				}
				else
				{
					UnitConversionKeySlot expectedSlot = new UnitConversionKeySlot(amount.Unit, toUnit);
					return Instance.conversions[expectedSlot].Convert(amount).ConvertedTo(toUnit);
				}
			}
			catch (KeyNotFoundException)
			{
				throw new UnitConversionException(amount.Unit, toUnit);
			}
		}