예제 #1
0
        private static int CompareValueTo <T>(this Argument <T> argument, object comparisonValue)
        {
            try
            {
                if (argument.Value is IComparable <T> && comparisonValue is T)
                {
                    return((argument.Value as IComparable <T>).CompareTo((T)comparisonValue));
                }

                if (argument.Value is IComparable)
                {
                    return((argument.Value as IComparable).CompareTo(comparisonValue));
                }
            }
            catch (ArgumentException)
            {
                var message = ExceptionMessages.Current.IncomparableTypes.Inject(
                    argument.GetType().FullName,
                    comparisonValue.GetType().FullName);
                throw ExceptionFactory.CreateArgumentException(argument, message);
            }

            throw new InvalidOperationException("This should not be possible, please report an issue at http://github.com/pmacn/Krav with the code used get this exception.");
        }
예제 #2
0
        public static Argument <T> IsInRange <T>(this Argument <T> argument, T min, T max)
            where T : IComparable <T>
        {
            if (argument.Value == null)
            {
                throw ExceptionFactory.CreateNullException(argument);
            }

            if (argument.CompareValueTo(min) < 0)
            {
                throw ExceptionFactory.OutOfRange(
                          argument,
                          ExceptionMessages.Current.NotInRange_TooLow.Inject(min, max));
            }

            if (argument.CompareValueTo(max) > 0)
            {
                throw ExceptionFactory.OutOfRange(
                          argument,
                          ExceptionMessages.Current.NotInRange_TooHigh.Inject(min, max));
            }

            return(argument);
        }