Exemple #1
0
        /// <summary>
        /// Create the syntax for a class constructor.
        /// </summary>
        /// <param name="className">Name of the class.</param>
        /// <param name="body">The generated body of the constructor.</param>
        /// <param name="parameters">A list with parameters.</param>
        /// <param name="modifiers">A list with modifiers.</param>
        /// <param name="attributes">A list with attributes.</param>
        /// <returns>The declaration syntax for a constructor.</returns>
        public static ConstructorDeclarationSyntax Create(
            string className,
            BlockSyntax body,
            IEnumerable <Parameter> parameters = null,
            IEnumerable <Modifiers> modifiers  = null,
            IEnumerable <Attribute> attributes = null)
        {
            if (className == null)
            {
                throw new ArgumentNullException(nameof(className));
            }

            var constructor = SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(className))
                              .WithBody(body);

            if (parameters != null)
            {
                constructor = constructor.WithParameterList(ParameterGenerator.Create(parameters.ToArray()));
            }

            if (attributes != null)
            {
                constructor = constructor.WithAttributeLists(AttributeGenerator.Create(attributes.ToArray()));
            }

            if (modifiers != null)
            {
                constructor = constructor.WithModifiers(ModifierGenerator.Create(modifiers.ToArray()));
            }

            return(constructor);
        }
Exemple #2
0
        /// <summary>
        /// Set method parameters.
        /// </summary>
        /// <param name="parameters">A set of wanted parameters.</param>
        /// <returns>The current method builder</returns>
        public MethodBuilder WithParameters(params Parameter[] parameters)
        {
            _parameters.Clear();
            foreach (var parameter in parameters)
            {
                _parameters.Add(ParameterGenerator.Create(parameter));
            }

            return(this);
        }
Exemple #3
0
        /// <summary>
        /// Create the syntax for a class constructor.
        /// </summary>
        /// <param name="className">Name of the class.</param>
        /// <param name="body">The generated body of the constructor.</param>
        /// <param name="parameters">A list with parameters.</param>
        /// <param name="modifiers">A list with modifiers.</param>
        /// <param name="attributes">A list with attributes.</param>
        /// <param name="constructorInitializer">The constructor initializer</param>
        /// <param name="summary">XML documentation summary</param>
        /// <returns>The declaration syntax for a constructor.</returns>
        public static ConstructorDeclarationSyntax Create(
            string className,
            BlockSyntax body,
            IEnumerable <Parameter> parameters            = null,
            IEnumerable <Modifiers> modifiers             = null,
            IEnumerable <Attribute> attributes            = null,
            ConstructorInitializer constructorInitializer = null,
            string summary = null)
        {
            if (className == null)
            {
                throw new ArgumentNullException(nameof(className));
            }

            var constructor = SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(className))
                              .WithBody(body);

            if (parameters != null)
            {
                constructor = constructor.WithParameterList(ParameterGenerator.Create(parameters.ToArray()));
            }

            if (constructorInitializer != null)
            {
                constructor = constructor.WithInitializer(
                    SyntaxFactory.ConstructorInitializer(
                        constructorInitializer.ConstructorInitializerType == ConstructorInitializerTypes.Base ? SyntaxKind.BaseConstructorInitializer : SyntaxKind.ThisConstructorInitializer,
                        constructorInitializer.Arguments == null ? null : ArgumentGenerator.Create(constructorInitializer.Arguments.ToArray())));
            }

            if (attributes != null)
            {
                constructor = constructor.WithAttributeLists(AttributeGenerator.Create(attributes.ToArray()));
            }

            if (modifiers != null)
            {
                constructor = constructor.WithModifiers(ModifierGenerator.Create(modifiers.ToArray()));
            }

            if (!string.IsNullOrEmpty(summary))
            {
                constructor = constructor.WithSummary(summary, parameters);
            }

            return(constructor);
        }
Exemple #4
0
        /// <summary>
        /// Set method parameters.
        /// </summary>
        /// <param name="parameters">A set of wanted parameters.</param>
        /// <returns>The current method builder</returns>
        public MethodBuilder WithParameters(params Parameter[] parameters)
        {
            _parameters.Clear();
            _parameterXmlDocumentation.Clear();

            foreach (var parameter in parameters)
            {
                _parameters.Add(ParameterGenerator.Create(parameter));

                if (parameter.XmlDocumentation != null)
                {
                    _parameterXmlDocumentation.Add(parameter);
                }
            }

            return(this);
        }
 public void Create_WhenProvidingParameterWithThisModifier_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("(thisinttest)", ParameterGenerator.Create(new Parameter("test", typeof(int), ParameterModifiers.This)).ToString());
 }
 public void Create_WhenNotProvidingAnyParameters_ShouldGetEmptyBraces()
 {
     Assert.AreEqual("()", ParameterGenerator.Create().ToString());
 }