Constraint

Namespace: Ecng.Reflection
We found 10 examples in language CSharp for this search. You will see 40 fragments of code.

        [Test]
        public void TestGetters ()
        {
            using (var constraint = FbxConstraintPosition.Create (Manager, "posConstraint")) {
                TestGetter (constraint.AffectX);
                TestGetter (constraint.AffectY);
                TestGetter (constraint.AffectZ);
                TestGetter (constraint.Translation);
            }
        }

        [Test]
        public void TestGetters ()
        {
            using (var constraint = FbxConstraintRotation.Create (Manager, "rConstraint")) {
                TestGetter (constraint.AffectX);
                TestGetter (constraint.AffectY);
                TestGetter (constraint.AffectZ);
                TestGetter (constraint.Rotation);
            }
        }

        [Test]
        public void TestGetters ()
        {
            using (var constraint = FbxConstraintScale.Create (Manager, "sConstraint")) {
                TestGetter (constraint.AffectX);
                TestGetter (constraint.AffectY);
                TestGetter (constraint.AffectZ);
                TestGetter (constraint.Scaling);
            }
        }

            /// <summary>
            /// Constructs a constraint required no other surfaces be included in this shape
            /// </summary>
            /// <returns>Constructed shape constraint</returns>
            public static ShapeConstraint Create_NoOtherSurface()
            {
                ShapeConstraint constraint = new ShapeConstraint();
                constraint.Type = ShapeConstraintType.NoOtherSurface;
                return constraint;
            }

            /// <summary>
            /// Constructs a constraint requiring the shape to be away from all walls
            /// </summary>
            /// <returns>Constructed shape constraint</returns>
            public static ShapeConstraint Create_AwayFromWalls()
            {
                ShapeConstraint constraint = new ShapeConstraint();
                constraint.Type = ShapeConstraintType.AwayFromWalls;
                return constraint;
            }


        [Fact]
        public void ResolveConstraint_RequiredConstraint_ResolvesCorrectly()
        {
            // Arrange & Act
            var constraint = _constraintResolver.ResolveConstraint("required");

            // Assert
            Assert.IsType<RequiredRouteConstraint>(constraint);
        }

        [Fact]
        public void ResolveConstraint_IntConstraint_ResolvesCorrectly()
        {
            // Arrange & Act
            var constraint = _constraintResolver.ResolveConstraint("int");

            // Assert
            Assert.IsType<IntRouteConstraint>(constraint);
        }

        [Fact]
        public void ResolveConstraint_AlphaConstraint()
        {
            // Arrange & Act
            var constraint = _constraintResolver.ResolveConstraint("alpha");

            // Assert
            Assert.IsType<AlphaRouteConstraint>(constraint);
        }

        [Fact]
        public void ResolveConstraint_RegexInlineConstraint_WithAComma_PassesAsASingleArgument()
        {
            // Arrange & Act
            var constraint = _constraintResolver.ResolveConstraint("regex(ab,1)");

            // Assert
            Assert.IsType<RegexInlineRouteConstraint>(constraint);
        }

        [Fact]
        public void ResolveConstraint_RegexInlineConstraint_WithCurlyBraces_Balanced()
        {
            // Arrange & Act
            var constraint = _constraintResolver.ResolveConstraint(
                @"regex(\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b)");

            // Assert
            Assert.IsType<RegexInlineRouteConstraint>(constraint);
        }

        [Fact]
        public void ResolveConstraint_BoolConstraint()
        {
            // Arrange & Act
            var constraint = _constraintResolver.ResolveConstraint("bool");

            // Assert
            Assert.IsType<BoolRouteConstraint>(constraint);
        }


        /// <summary>
        /// Adds the constraint to the collection.
        /// </summary>
        public void Add(Constraint constraint) => Add(constraint, true);

        private void AddForeignKeyConstraint(ForeignKeyConstraint constraint)
        {
            if (!constraint.CanEnableConstraint())
            {
                throw ExceptionBuilder.ConstraintParentValues();
            }
            constraint.CheckCanAddToCollection(this);
        }

        private bool AutoGenerated(Constraint constraint)
        {
            ForeignKeyConstraint fk = (constraint as ForeignKeyConstraint);
            if (null != fk)
            {
                return XmlTreeGen.AutoGenerated(fk, false);
            }
            else
            {
                UniqueConstraint unique = (UniqueConstraint)constraint;
                return XmlTreeGen.AutoGenerated(unique);
            }
        }

        /// <summary>
        ///  Adds the constraint to the constraints array.
        /// </summary>
        private void ArrayAdd(Constraint constraint)
        {
            Debug.Assert(constraint != null, "Attempt to add null constraint to constraint array");
            List.Add(constraint);
        }

        private void ArrayRemove(Constraint constraint)
        {
            List.Remove(constraint);
        }

        /// <summary>
        /// Indicates if a <see cref='System.Data.Constraint'/> can be removed.
        /// </summary>
        // PUBLIC because called by design-time... need to consider this.
        public bool CanRemove(Constraint constraint)
        {
            return CanRemove(constraint, fThrowException: false);
        }

            /// <summary>
            /// Constructs a constraint required no other surfaces be included in this shape
            /// </summary>
            /// <returns>Constructed shape constraint</returns>
            public static ShapeConstraint Create_NoOtherSurface()
            {
                ShapeConstraint constraint = new ShapeConstraint();
                constraint.Type = ShapeConstraintType.NoOtherSurface;
                return constraint;
            }

            /// <summary>
            /// Constructs a constraint requiring the shape to be away from all walls
            /// </summary>
            /// <returns>Constructed shape constraint</returns>
            public static ShapeConstraint Create_AwayFromWalls()
            {
                ShapeConstraint constraint = new ShapeConstraint();
                constraint.Type = ShapeConstraintType.AwayFromWalls;
                return constraint;
            }

        #endregion

        #region Constraints with an expected value

        #region Equality and Identity
        /// <summary>
        /// Resolves the chain of constraints using an
        /// EqualConstraint as base.
        /// </summary>
        public Constraint EqualTo(object expected)
        {
            return Resolve(new EqualConstraint(expected));
        }

        /// <summary>
        /// Resolves the chain of constraints using a
        /// SameAsConstraint as base.
        /// </summary>
        public Constraint SameAs(object expected)
        {
            return Resolve(new SameAsConstraint(expected));
        }
        #endregion

        #region Comparison Constraints
        /// <summary>
        /// Resolves the chain of constraints using a
        /// LessThanConstraint as base.
        /// </summary>
        public Constraint LessThan(IComparable expected)
        {
            return Resolve(new LessThanConstraint(expected));
        }

        /// <summary>
        /// Resolves the chain of constraints using a
        /// GreaterThanConstraint as base.
        /// </summary>
        public Constraint GreaterThan(IComparable expected)
        {
            return Resolve(new GreaterThanConstraint(expected));
        }

        /// <summary>
        /// Resolves the chain of constraints using a
        /// LessThanOrEqualConstraint as base.
        /// </summary>
        public Constraint LessThanOrEqualTo(IComparable expected)
        {
            return Resolve(new LessThanOrEqualConstraint(expected));
        }

        /// <summary>
        /// Resolves the chain of constraints using a
        /// LessThanOrEqualConstraint as base.
        /// </summary>
        public Constraint AtMost(IComparable expected)
        {
            return Resolve(new LessThanOrEqualConstraint(expected));
        }


		private void _handleBeforeConstraintNameChange (object sender, string newName)
		{
			if (newName == null || newName == "")
				throw new ArgumentException (
					"ConstraintName cannot be set to null or empty after adding it to a ConstraintCollection.");

			if (_isDuplicateConstraintName (newName, (Constraint) sender))
				throw new DuplicateNameException ("Constraint name already exists.");
		}

		private bool _isDuplicateConstraintName (string constraintName, Constraint excludeFromComparison)
		{
			foreach (Constraint cst in List) {
				if (cst == excludeFromComparison)
					continue;
				if (String.Compare (constraintName, cst.ConstraintName, false, Table.Locale) == 0)
					return true;
			}

			return false;
		}

		public
#if !NET_2_0
		virtual
#endif
		Constraint Add (string name, DataColumn column, bool primaryKey)
		{
			UniqueConstraint uc = new UniqueConstraint (name, column, primaryKey);
			Add (uc);
			return uc;
		}

		public
#if !NET_2_0
		virtual
#endif
		Constraint Add (string name, DataColumn primaryKeyColumn, DataColumn foreignKeyColumn)
		{
			ForeignKeyConstraint fc = new ForeignKeyConstraint (name, primaryKeyColumn, foreignKeyColumn);
			Add (fc);
			return fc;
		}

		public
#if !NET_2_0
		virtual
#endif
		Constraint Add (string name, DataColumn[] columns, bool primaryKey)
		{
			UniqueConstraint uc = new UniqueConstraint (name, columns, primaryKey);
			Add (uc);
			return uc;
		}

		public
#if !NET_2_0
		virtual
#endif
		Constraint Add (string name, DataColumn[] primaryKeyColumns, DataColumn[] foreignKeyColumns)
		{
			ForeignKeyConstraint fc = new ForeignKeyConstraint (name, primaryKeyColumns, foreignKeyColumns);
			Add (fc);
			return fc;
		}


        [Fact]
        public void ParseRouteParameter_WithoutAConstraintName()
        {
            // Arrange & Act
            var templatePart = ParseParameter("param:");

            // Assert
            Assert.Equal("param", templatePart.Name);
            Assert.Null(templatePart.DefaultValue);
            var constraint = Assert.Single(templatePart.InlineConstraints);
            Assert.Empty(constraint.Constraint);
        }

        [Fact]
        public void ParseRouteParameter_WithoutAConstraintNameOrParameterName()
        {
            // Arrange & Act
            var templatePart = ParseParameter("param:=");

            // Assert
            Assert.Equal("param", templatePart.Name);
            Assert.Equal("", templatePart.DefaultValue);
            var constraint = Assert.Single(templatePart.InlineConstraints);
            Assert.Empty(constraint.Constraint);
        }

        [Fact]
        public void ParseRouteParameter_ConstraintAndDefault_ParsedCorrectly()
        {
            // Arrange & Act
            var templatePart = ParseParameter("param:int=111111");

            // Assert
            Assert.Equal("param", templatePart.Name);
            Assert.Equal("111111", templatePart.DefaultValue);

            var constraint = Assert.Single(templatePart.InlineConstraints);
            Assert.Equal("int", constraint.Constraint);
        }

        [Fact]
        public void ParseRouteParameter_ConstraintAndOptional_ParsedCorrectly()
        {
            // Arrange & Act
            var templatePart = ParseParameter(@"param:int?");

            // Assert
            Assert.Equal("param", templatePart.Name);
            Assert.True(templatePart.IsOptional);

            var constraint = Assert.Single(templatePart.InlineConstraints);
            Assert.Equal("int", constraint.Constraint);
        }

        [Fact]
        public void ParseRouteParameter_ConstraintWithArgumentsAndOptional_ParsedCorrectly()
        {
            // Arrange & Act
            var templatePart = ParseParameter(@"param:test(\d+)?");

            // Assert
            Assert.Equal("param", templatePart.Name);
            Assert.True(templatePart.IsOptional);

            var constraint = Assert.Single(templatePart.InlineConstraints);
            Assert.Equal(@"test(\d+)", constraint.Constraint);
        }

        [Fact]
        public void ParseRouteParameter_ConstraintWithClosingBraceInPattern_ClosingBraceIsParsedCorrectly()
        {
            // Arrange & Act
            var templatePart = ParseParameter(@"param:test(\})");

            // Assert
            Assert.Equal("param", templatePart.Name);

            var constraint = Assert.Single(templatePart.InlineConstraints);
            Assert.Equal(@"test(\})", constraint.Constraint);
        }


        private void Add(string key, IRouteConstraint constraint)
        {
            if (!_constraints.TryGetValue(key, out var list))
            {
                list = new List<IRouteConstraint>();
                _constraints.Add(key, list);
            }

            list.Add(constraint);
        }

            /// <summary>
            /// Constructs a constraint required no other surfaces be included in this shape
            /// </summary>
            /// <returns>Constructed shape constraint</returns>
            public static ShapeConstraint Create_NoOtherSurface()
            {
                ShapeConstraint constraint = new ShapeConstraint();
                constraint.Type = ShapeConstraintType.NoOtherSurface;
                return constraint;
            }

            /// <summary>
            /// Constructs a constraint requiring the shape to be away from all walls
            /// </summary>
            /// <returns>Constructed shape constraint</returns>
            public static ShapeConstraint Create_AwayFromWalls()
            {
                ShapeConstraint constraint = new ShapeConstraint();
                constraint.Type = ShapeConstraintType.AwayFromWalls;
                return constraint;
            }

Ecng.Reflection.Emit.Constraint : Object

Constructors :

public Constraint()
public Constraint(Type baseType = )
public Constraint(GenericParameterAttributes attribute = )

Methods :

public Type get_BaseType()
public Void set_BaseType(Type value = )
public GenericParameterAttributes get_Attribute()
public Void set_Attribute(GenericParameterAttributes value = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()