Exemple #1
0
        //[DebuggerStepThrough]
        ////[DebuggerHidden]
        public List <ConstraintViolation> CheckConstraints(object value)
        {
            List <ConstraintViolation> violations = null;

            // Check optional first - this avoids NPE's in constraints
            if (this.optional)
            {
                if (value == null)
                {
                    violations = new List <ConstraintViolation>();
                }
            }
            else
            {
                if (value == null)
                {
                    violations = new List <ConstraintViolation>
                    {
                        new ConstraintViolation(this.name, NotOptionalConstraint.Instance, null)
                    };
                }
            }

            if (violations == null)
            {
                foreach (ConstraintInstance constraint in this.constraints)
                {
                    bool valid;
                    try
                    {
                        valid = constraint.IsValid(value);
                    }
                    catch (NullReferenceException e)
                    {
                        // A NPE is the same as a failing constraint
                        valid = false;
                    }

                    if (!valid)
                    {
                        if (violations == null)
                        {
                            violations = new List <ConstraintViolation>();
                        }
                        var violation = new ConstraintViolation(this.name, constraint.Annotation, value);
                        violations.Add(violation);
                    }
                }
            }

            if (violations == null)
            {
                violations = new List <ConstraintViolation>();
            }

            return(violations);
        }
        //[DebuggerStepThrough]
        ////[DebuggerHidden]
        public List<ConstraintViolation> CheckConstraints(object value)
        {
            List<ConstraintViolation> violations = null;

            // Check optional first - this avoids NPE's in constraints
            if (this.optional)
            {
                if (value == null)
                {
                    violations = new List<ConstraintViolation>();
                }
            }
            else
            {
                if (value == null)
                {
                    violations = new List<ConstraintViolation>
                                     {
                                             new ConstraintViolation(this.name, NotOptionalConstraint.Instance, null)
                                     };
                }
            }

            if (violations == null)
            {
                foreach (ConstraintInstance constraint in this.constraints)
                {
                    bool valid;
                    try
                    {
                        valid = constraint.IsValid(value);
                    }
                    catch (NullReferenceException e)
                    {
                        // A NPE is the same as a failing constraint
                        valid = false;
                    }

                    if (!valid)
                    {
                        if (violations == null)
                        {
                            violations = new List<ConstraintViolation>();
                        }
                        var violation = new ConstraintViolation(this.name, constraint.Annotation, value);
                        violations.Add(violation);
                    }
                }
            }

            if (violations == null)
            {
                violations = new List<ConstraintViolation>();
            }

            return violations;
        }