Example #1
0
        public void TestConvertChecked()
        {
            NumberHelper.CheckedConvert <double>(20L).ShouldEqual(20.0);
            NumberHelper.CheckedConvert <int?>(10M).ShouldEqual(10);
            NumberHelper.CheckedConvert <decimal>(1.3f).ShouldEqual(1.3M);
            NumberHelper.CheckedConvert <int>((long)int.MaxValue).ShouldEqual(int.MaxValue);
            NumberHelper.CheckedConvert <int?>(null).ShouldEqual(null);

            UnitTestHelpers.AssertThrows <InvalidCastException>(() => NumberHelper.CheckedConvert <int>(1.5));
            UnitTestHelpers.AssertThrows <InvalidCastException>(() => NumberHelper.CheckedConvert <int>(1.111111e5f));
            UnitTestHelpers.AssertThrows <InvalidCastException>(() => NumberHelper.CheckedConvert <int>(-.1M));
            UnitTestHelpers.AssertThrows <InvalidCastException>(() => NumberHelper.CheckedConvert <int>(1.5));
            UnitTestHelpers.AssertThrows <OverflowException>(() => NumberHelper.CheckedConvert <byte>(-1L));
            UnitTestHelpers.AssertThrows <OverflowException>(() => NumberHelper.CheckedConvert <int?>(2.0 * int.MaxValue));
            UnitTestHelpers.AssertThrows <InvalidCastException>(() => NumberHelper.CheckedConvert <int>(null));
        }
Example #2
0
        /// <summary>
        /// Gets the strongly-typed value of the named property. This method can be used in OData queries as long as <paramref name="propertyName"/>
        /// is a constant or local variable capture
        /// </summary>
        /// <typeparam name="TProperty">the property type</typeparam>
        /// <param name="propertyName">the property name</param>
        /// <returns>the property value</returns>
        public TProperty Get <TProperty>(string propertyName)
        {
            Throw.IfNull(propertyName, "propertyName");

            object result;

            if (!this.Values.TryGetValue(propertyName, out result))
            {
                throw new ArgumentException("The entity does not contain a value for property '" + propertyName + "'!");
            }

            if (result == null)
            {
                if (!typeof(TProperty).CanBeNull())
                {
                    throw new InvalidCastException(string.Format(
                                                       "Property '{0}' has a null value and cannot be cast to requested type '{1}'",
                                                       propertyName,
                                                       typeof(TProperty)
                                                       ));
                }
                return(default(TProperty)); // always null due to check above
            }

            if (result is TProperty)
            {
                return((TProperty)result);
            }

            var tProperty = typeof(TProperty);

            if (tProperty.IsNumeric() && result.GetType().IsNumeric())
            {
                try
                {
                    return(NumberHelper.CheckedConvert <TProperty>(result));
                }
                catch (Exception ex)
                {
                    throw new InvalidCastException(
                              string.Format(
                                  "Failed to convert property '{0}' value '{1}' of type {2} to requested type {3}",
                                  propertyName,
                                  result,
                                  result.GetType(),
                                  tProperty
                                  ),
                              ex
                              );
                }
            }

            if (typeof(ODataObject).IsAssignableFrom(tProperty))
            {
                // at this point, we already know it's not ODataEntity since that would be
                // handled above. Thus, we can just handle ODataValue
                return((TProperty)(object)ODataValue.FromObject(result));
            }

            throw new InvalidCastException(string.Format(
                                               "value '{0}' of type {1} for property '{2}' is not compatible with requested type {3}",
                                               result,
                                               result.GetType(), // already checked for null above!
                                               propertyName,
                                               tProperty
                                               ));
        }