private static TAttribute GetAttribute <TAttribute>(QuantityType expectedType, ITypeDescriptorContext context) where TAttribute : UnitAttributeBase
        {
            TAttribute          attribute = null;
            AttributeCollection ua        = context?.PropertyDescriptor.Attributes;

            attribute = (TAttribute)ua?[typeof(TAttribute)];

            if (attribute != null)
            {
                QuantityType expected = expectedType;
                QuantityType actual   = QuantityType.Undefined;

                if (attribute.UnitType != null)
                {
                    actual = Quantity.From(1, attribute.UnitType).Type;
                }
                if (actual != QuantityType.Undefined && expected != actual)
                {
                    throw new ArgumentException($"The specified UnitType:'{attribute.UnitType}' dose not match QuantityType:'{expected}'");
                }
            }

            return(attribute);
        }
Esempio n. 2
0
 /// <summary>
 ///     Convert between any two quantity units given a numeric value and two unit enum values.
 /// </summary>
 /// <param name="fromValue">Numeric value.</param>
 /// <param name="fromUnitValue">From unit enum value.</param>
 /// <param name="toUnitValue">To unit enum value, must be compatible with <paramref name="fromUnitValue" />.</param>
 /// <returns>The converted value in the new unit representation.</returns>
 public static double Convert(QuantityValue fromValue, Enum fromUnitValue, Enum toUnitValue)
 {
     return(Quantity
            .From(fromValue, fromUnitValue)
            .As(toUnitValue));
 }
Esempio n. 3
0
 /// <summary>Computes the min of a sequence of <typeparamref name="TQuantity" /> values.</summary>
 /// <param name="source">A sequence of <typeparamref name="TQuantity" /> values to calculate the min of.</param>
 /// <param name="unitType">The desired unit type for the resulting quantity</param>
 /// <returns>The min of the values in the sequence, represented in the specified unit type.</returns>
 /// <exception cref="T:System.ArgumentNullException">
 ///     <paramref name="source">source</paramref> is null.
 /// </exception>
 /// <exception cref="T:System.InvalidOperationException"><paramref name="source">source</paramref> contains no elements.</exception>
 /// <exception cref="ArgumentException">
 ///     <paramref name="source">source</paramref> contains quantity types different from <paramref name="unitType" />.
 /// </exception>
 public static TQuantity Min <TQuantity>(this IEnumerable <TQuantity> source, Enum unitType)
     where TQuantity : IQuantity
 {
     return((TQuantity)Quantity.From(source.Min(x => x.As(unitType)), unitType));
 }
Esempio n. 4
0
 /// <summary>Returns the absolute value of a <typeparamref name="TQuantity" />.</summary>
 /// <param name="value">
 ///     A quantity with a value that is greater than or equal to <see cref="F:System.Double.MinValue" />,
 ///     but less than or equal to <see cref="F:System.Double.MaxValue" />.
 /// </param>
 /// <returns>A quantity with a value, such that 0 ≤ value ≤ <see cref="F:System.Double.MaxValue" />.</returns>
 public static TQuantity Abs <TQuantity>(this TQuantity value) where TQuantity : IQuantity
 {
     return(value.Value >= 0 ? value : (TQuantity)Quantity.From(-value.Value, value.Unit));
 }
Esempio n. 5
0