AddConstraint() public method

Adds a constraint for the type parameter.
public AddConstraint ( string constraint ) : void
constraint string The constraint.
return void
Example #1
0
        public void CloneTest()
        {
            TypeParameter typeParameter = new TypeParameter();
            typeParameter.Name = "T";
            typeParameter.AddConstraint("IDisposable");
            typeParameter.AddConstraint("new()");

            TypeParameter clone = typeParameter.Clone() as TypeParameter;
            Assert.IsNotNull(clone, "Clone should return a TypeParameter instance.");

            Assert.AreEqual(typeParameter.Name, clone.Name, "Name property was not copied correctly.");
            Assert.AreEqual(typeParameter.Constraints.Count, clone.Constraints.Count, "Constraints property was not copied correctly.");
            Assert.AreEqual(typeParameter.Constraints[0], clone.Constraints[0], "Constraints property was not copied correctly.");
            Assert.AreEqual(typeParameter.Constraints[1], clone.Constraints[1], "Constraints property was not copied correctly.");
        }
Example #2
0
        /// <summary>
        /// Creates a clone of this instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public object Clone()
        {
            TypeParameter clone = new TypeParameter();

            //
            // Copy state
            //
            clone._name = _name;
            foreach (string constraint in Constraints)
            {
                clone.AddConstraint(constraint);
            }

            return(clone);
        }
Example #3
0
        /// <summary>
        /// Parses type parameters.
        /// </summary>
        /// <param name="genericElement">The generic element.</param>
        private void ParseTypeParameters(IGenericElement genericElement)
        {
            EatWhiteSpace();

            if (NextChar == VBSymbol.EndParameterList ||
                NextChar == EmptyChar)
            {
                this.OnParseError("Expected type parameter");
            }

            while (NextChar != VBSymbol.EndParameterList &&
                NextChar != EmptyChar)
            {
                if (genericElement.TypeParameters.Count > 0 && NextChar == VBSymbol.AliasSeparator)
                {
                    TryReadChar();
                }

                string typeParameterName = CaptureWord();

                EatWhiteSpace();

                if (NextChar == EmptyChar)
                {
                    break;
                }

                TypeParameter typeParameter = new TypeParameter();
                typeParameter.Name = typeParameterName;

                if (NextChar != VBSymbol.AliasSeparator &&
                    NextChar != VBSymbol.EndParameterList)
                {
                    if (char.ToLower(NextChar) == char.ToLower(VBKeyword.As[0]))
                    {
                        TryReadChar();

                        if (char.ToLower(NextChar) == char.ToLower(VBKeyword.As[1]))
                        {
                            TryReadChar();

                            EatWhiteSpace();

                            if (NextChar == VBSymbol.EndParameterList)
                            {
                                this.OnParseError("Expected type parameter constraint");
                            }

                            if (NextChar == VBSymbol.BeginTypeConstraintList)
                            {
                                TryReadChar();

                                while (NextChar != VBSymbol.EndTypeConstraintList &&
                                    NextChar != EmptyChar)
                                {
                                    string typeParameterConstraint;
                                    typeParameterConstraint = ParseTypeParameterConstraint();
                                    typeParameter.AddConstraint(typeParameterConstraint);
                                }

                                EatChar(VBSymbol.EndTypeConstraintList);
                            }
                            else
                            {
                                while (NextChar != VBSymbol.EndParameterList &&
                                    NextChar != EmptyChar)
                                {
                                    string typeParameterConstraint;
                                    typeParameterConstraint = ParseTypeParameterConstraint();
                                    typeParameter.AddConstraint(typeParameterConstraint);
                                }
                            }
                        }
                    }
                }

                genericElement.AddTypeParameter(typeParameter);
            }

            EatChar(VBSymbol.EndParameterList);
        }
Example #4
0
        /// <summary>
        /// Creates a clone of this instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public object Clone()
        {
            TypeParameter clone = new TypeParameter();

            //
            // Copy state
            //
            clone._name = _name;
            foreach (string constraint in Constraints)
            {
                clone.AddConstraint(constraint);
            }

            return clone;
        }