コード例 #1
0
        public static Argument <Guid> IsNotEmpty(this Argument <Guid> argument)
        {
            if (Guid.Empty.Equals(argument.Value))
            {
                throw ExceptionFactory.CreateArgumentException(argument, ExceptionMessages.Current.EmptyGuid);
            }

            return(argument);
        }
コード例 #2
0
        public static Argument <double> IsANumber(this Argument <double> argument)
        {
            if (Double.IsNaN(argument.Value))
            {
                throw ExceptionFactory.CreateArgumentException(argument,
                                                               ExceptionMessages.Current.IsNotANumber);
            }

            return(argument);
        }
コード例 #3
0
        public static Argument <string> IsNotNullOrWhiteSpace(this Argument <string> argument)
        {
            argument.IsNotNullOrEmpty();
            if (argument.Value.Any(char.IsWhiteSpace))
            {
                throw ExceptionFactory.CreateArgumentException(argument, ExceptionMessages.Current.WasWhiteSpace);
            }

            return(argument);
        }
コード例 #4
0
        public Argument <T> IsOfType(Type expectedType)
        {
            var actualType = this.Value == null ? typeof(T) : this.Value.GetType();

            if (!expectedType.IsAssignableFrom(actualType))
            {
                throw ExceptionFactory.CreateArgumentException(
                          this,
                          ExceptionMessages.Current.NotOfType.Inject(expectedType.FullName, actualType.FullName));
            }

            return(this);
        }
コード例 #5
0
        public static Argument <string> IsNotNullOrEmpty(this Argument <string> argument)
        {
            if (argument.Value == null)
            {
                throw ExceptionFactory.CreateNullException(argument);
            }

            if (argument.Value.Length == 0)
            {
                throw ExceptionFactory.CreateArgumentException(argument, ExceptionMessages.Current.WasEmptyString);
            }

            return(argument);
        }
コード例 #6
0
        public static Argument <T> DoesNotContainNull <T>(this Argument <T> argument)
            where T : IEnumerable
        {
            if (argument.Value == null)
            {
                throw ExceptionFactory.CreateNullException(argument);
            }

            if (argument.Value.Cast <object>().Any(o => o == null))
            {
                throw ExceptionFactory.CreateArgumentException(argument, ExceptionMessages.Current.ContainedNull);
            }

            return(argument);
        }
コード例 #7
0
        public static Argument <T> IsNotEmpty <T>(this Argument <T> argument)
            where T : IEnumerable
        {
            if (argument.Value == null)
            {
                throw ExceptionFactory.CreateNullException(argument);
            }

            if (!argument.Value.GetEnumerator().MoveNext())
            {
                throw ExceptionFactory.CreateArgumentException(argument, ExceptionMessages.Current.EmptyCollection);
            }

            return(argument);
        }
コード例 #8
0
ファイル: TypeArgumentExtensions.cs プロジェクト: Miista/Krav
        public static Argument <Type> Is(this Argument <Type> argument, Type expectedType)
        {
            if (argument.Value == null)
            {
                throw ExceptionFactory.CreateNullException(argument);
            }

            if (!expectedType.IsAssignableFrom(argument.Value))
            {
                throw ExceptionFactory.CreateArgumentException(
                          argument,
                          ExceptionMessages.Current.IsNotExpectedType.Inject(expectedType.FullName, argument.Value.FullName));
            }

            return(argument);
        }
コード例 #9
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.");
        }