Esempio n. 1
0
        public void NotContains(Expression <Func <object> > expression, Type enumType)
        {
            this.EnsureNotNull("expression", (object)expression);
            this.EnsureNotNullParameter((Expression <Func <object> >)(() => enumType));
            string name      = ParamExpression.GetName((LambdaExpression)expression);
            object obj       = ParamExpression.GetValue((LambdaExpression)expression);
            Type   enumType1 = enumType;
            bool   flag      = false;

            if (enumType.IsGenericType)
            {
                if (!enumType.Assembly.FullName.Equals(typeof(Nullable <>).Assembly.FullName) || !enumType.Namespace.Equals(typeof(Nullable <>).Namespace) || !enumType.Name.Equals(typeof(Nullable <>).Name))
                {
                    throw new ArgumentException("enumType debe ser una enumeración", "enumType");
                }
                flag      = true;
                enumType1 = Nullable.GetUnderlyingType(enumType);
            }
            if (!enumType1.IsEnum)
            {
                throw new ArgumentException("enumType debe ser una enumeración", "enumType");
            }
            Exception exception = (Exception)null;

            if (!flag && obj == null)
            {
                exception = WhenBase <TException> .CreateException(StringExtensions.StringFormat("El valor de '{0}' no puedo ser nulo si el tipo no es nulable", (object)name));
            }
            else if (!Enumerable.Any <FlagsAttribute>(Enumerable.OfType <FlagsAttribute>((IEnumerable)enumType1.GetCustomAttributes(false))))
            {
                if (obj != null && !Enum.IsDefined(enumType1, obj))
                {
                    exception = WhenBase <TException> .CreateException(StringExtensions.StringFormat("El valor de '{0}' no es un valor posible", (object)name));
                }
            }
            else
            {
                IEnumerable <long> enumerable = Enumerable.Select <object, long>(Enumerable.Cast <object>((IEnumerable)Enum.GetValues(enumType1)), (Func <object, long>)(x => (long)Convert.ChangeType(x, typeof(long))));
                long num1 = 0;
                long num2 = (long)Convert.ChangeType(obj, typeof(long));
                foreach (long num3 in enumerable)
                {
                    if ((num3 & num2) == num3)
                    {
                        num1 += num3;
                    }
                }
                if (num1 != num2)
                {
                    exception = WhenBase <TException> .CreateException(StringExtensions.StringFormat("El valor de '{0}' no es un valor posible", (object)name));
                }
            }
            if (exception != null)
            {
                throw exception;
            }
        }
Esempio n. 2
0
        public void IsNotValidIdentifier(Expression <Func <string> > expression)
        {
            this.EnsureNotNull("expression", (object)expression);
            string name    = ParamExpression.GetName((LambdaExpression)expression);
            string value   = ParamExpression.GetValue((LambdaExpression)expression) as string;
            string message = StringExtensions.StringFormat("El valor de '{0}' no es un identificador válido", (object)name);

            this.CheckCondition((Func <bool>)(() => value == null || !CodeGenerator.IsValidLanguageIndependentIdentifier(value)), message);
        }
Esempio n. 3
0
        public void IsTooLong(Expression <Func <string> > expression, int maxLength)
        {
            this.EnsureNotNull("expression", (object)expression);
            string name    = ParamExpression.GetName((LambdaExpression)expression);
            string value   = ParamExpression.GetValue((LambdaExpression)expression) as string;
            string message = StringExtensions.StringFormat("El valor de '{0}' es demasiado largo (máximo {1})", (object)name, (object)maxLength);

            this.CheckCondition((Func <bool>)(() => value != null && value.Length > maxLength), message);
        }
Esempio n. 4
0
        public void IsNullOrWhiteSpace(Expression <Func <string> > expression)
        {
            this.EnsureNotNull("expression", (object)expression);
            string name    = ParamExpression.GetName((LambdaExpression)expression);
            string value   = ParamExpression.GetValue((LambdaExpression)expression) as string;
            string message = StringExtensions.StringFormat("El valor de '{0}' no puede ser nulo, cadena vacía o todo espacios", (object)name);

            this.CheckCondition((Func <bool>)(() => string.IsNullOrWhiteSpace(value)), message);
        }
Esempio n. 5
0
        public void IsNotHexString(Expression <Func <string> > expression)
        {
            this.EnsureNotNull("expression", (object)expression);
            string name    = ParamExpression.GetName((LambdaExpression)expression);
            string value   = ParamExpression.GetValue((LambdaExpression)expression) as string;
            string message = StringExtensions.StringFormat("El valor de '{0}' tiene que ser una cadena hexadecimal", (object)name);

            this.CheckCondition((Func <bool>)(() => value != null && Enumerable.Any <char>((IEnumerable <char>)value.ToUpper(), (Func <char, bool>)(c => !Uri.IsHexDigit(c)))), message);
        }
Esempio n. 6
0
        public void IsNull(Expression <Func <object> > expression)
        {
            this.EnsureNotNull("expression", (object)expression);
            string name    = ParamExpression.GetName((LambdaExpression)expression);
            object value   = ParamExpression.GetValue((LambdaExpression)expression);
            string message = StringExtensions.StringFormat("El valor de '{0}' no puede ser nulo", (object)name);

            this.CheckCondition((Func <bool>)(() => value == null), message);
        }
Esempio n. 7
0
        public void NotEquals(Expression <Func <object> > expression, object expectedValue)
        {
            this.EnsureNotNull("expression", (object)expression);
            string name    = ParamExpression.GetName((LambdaExpression)expression);
            object value   = ParamExpression.GetValue((LambdaExpression)expression);
            string message = StringExtensions.StringFormat("El valor de '{0}' tiene que ser {1}", (object)name, expectedValue);

            this.CheckCondition((Func <bool>)(() => value == null && expectedValue != null || value != null && !value.Equals(expectedValue)), message);
        }
Esempio n. 8
0
        public void IsNullOrEmpty(Expression <Func <IEnumerable> > expression)
        {
            this.EnsureNotNull("expression", (object)expression);
            string      name    = ParamExpression.GetName((LambdaExpression)expression);
            IEnumerable value   = ParamExpression.GetValue((LambdaExpression)expression) as IEnumerable;
            string      message = StringExtensions.StringFormat("El valor de '{0}' no puede ser nulo o una lista vacía", (object)name);

            this.CheckCondition((Func <bool>)(() => value == null || !Enumerable.Any <object>(Enumerable.Cast <object>(value))), message);
        }
Esempio n. 9
0
        protected void EnsureNotNullParameter(Expression <Func <object> > expression)
        {
            this.EnsureNotNull("expression", (object)expression);
            string name = ParamExpression.GetName((LambdaExpression)expression);

            if (ParamExpression.GetValue((LambdaExpression)expression) == null)
            {
                throw new ArgumentNullException(StringExtensions.StringFormat("El argumento '{0}' no puede ser nulo", (object)name));
            }
        }
Esempio n. 10
0
        private void PrivateIsZero(LambdaExpression expression)
        {
            this.EnsureNotNull("expression", (object)expression);
            string      name    = ParamExpression.GetName(expression);
            IComparable value   = ParamExpression.GetValue(expression) as IComparable;
            string      message = StringExtensions.StringFormat("El valor de '{0}' no puede ser 0", (object)name);
            object      zero    = Activator.CreateInstance(value.GetType());

            this.CheckCondition((Func <bool>)(() => value != null && value.CompareTo(zero) == 0), message);
        }
Esempio n. 11
0
 public static string NameOf <T>(Expression <Func <T, object> > propertyExpression)
 {
     return(ParamExpression.GetName((LambdaExpression)propertyExpression));
 }