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);
        }
        /// <summary>
        /// Create the syntax for a field of a class.
        /// </summary>
        /// <param name="field">Field to create.</param>
        /// <returns>The declaration syntax for a field.</returns>
        public static FieldDeclarationSyntax Create(Field field)
        {
            if (field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            var variableDeclarator = VariableDeclarator(Identifier(field.Name));

            if (field.InitializeWith != null)
            {
                variableDeclarator = variableDeclarator.WithInitializer(EqualsValueClause(field.InitializeWith));
            }

            var fieldDeclaration = FieldDeclaration(VariableDeclaration(TypeGenerator.Create(field.Type), SeparatedList(new[] { variableDeclarator })));

            if (field.Modifiers != null)
            {
                fieldDeclaration = fieldDeclaration.WithModifiers(ModifierGenerator.Create(field.Modifiers.ToArray()));
            }

            if (field.Attributes != null)
            {
                fieldDeclaration = fieldDeclaration.WithAttributeLists(AttributeGenerator.Create(field.Attributes.ToArray()));
            }

            return(fieldDeclaration);
        }
Exemple #3
0
        /// <summary>
        /// Create the syntax for a property of a class.
        /// </summary>
        /// <param name="property">The property to create.</param>
        /// <returns>The declaration syntax for a property.</returns>
        public static PropertyDeclarationSyntax Create(Property property)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            PropertyDeclarationSyntax propertyDeclaration;

            if (property is AutoProperty)
            {
                propertyDeclaration = CreateAutoProperty((AutoProperty)property);
            }
            else if (property is BodyProperty)
            {
                propertyDeclaration = CreateBodyProperty((BodyProperty)property);
            }
            else
            {
                throw new ArgumentException($"Unknown property type: {property.Type}, could not generate code.");
            }

            if (property.Modifiers != null)
            {
                propertyDeclaration = propertyDeclaration.WithModifiers(ModifierGenerator.Create(property.Modifiers.ToArray()));
            }

            if (property.Attributes != null)
            {
                propertyDeclaration = propertyDeclaration.WithAttributeLists(AttributeGenerator.Create(property.Attributes.ToArray()));
            }

            return(propertyDeclaration);
        }
Exemple #4
0
        private MethodDeclarationSyntax BuildModifiers(MethodDeclarationSyntax method)
        {
            if (_modifiers == null || !_modifiers.Any())
            {
                return(method);
            }

            return(method.WithModifiers(ModifierGenerator.Create(_modifiers.ToArray())));
        }
Exemple #5
0
        private static AccessorDeclarationSyntax CreateAccessDeclaration(SyntaxKind kind, BlockSyntax body, IEnumerable <Modifiers> modifiers)
        {
            var accessDeclaration = AccessorDeclaration(kind);

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

            accessDeclaration = body != null?accessDeclaration.WithBody(body) : accessDeclaration.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));

            return(accessDeclaration);
        }
Exemple #6
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);
        }
        private ClassDeclarationSyntax BuildClassBase()
        {
            if (_inheritance.Any())
            {
                var syntaxNodeOrToken = new SyntaxNodeOrToken[(_inheritance.Count * 2) - 1];
                for (int n = 0; n < (_inheritance.Count * 2) - 1; n += 2)
                {
                    syntaxNodeOrToken[n] = SyntaxFactory.SimpleBaseType(TypeGenerator.Create(_inheritance[n]));
                    if (n + 1 < _inheritance.Count - 1)
                    {
                        syntaxNodeOrToken[n + 1] = SyntaxFactory.Token(SyntaxKind.CommaToken);
                    }
                }

                return(SyntaxFactory.ClassDeclaration(_name).WithBaseList(SyntaxFactory.BaseList(SyntaxFactory.SeparatedList <BaseTypeSyntax>(syntaxNodeOrToken))).WithModifiers(ModifierGenerator.Create(_modifiers.ToArray())));
            }

            return(SyntaxFactory.ClassDeclaration(_name).WithModifiers(ModifierGenerator.Create(_modifiers.ToArray())));
        }
Exemple #8
0
 protected SyntaxTokenList CreateModifiers()
 {
     return(ModifierGenerator.Create(_modifiers.ToArray()));
 }
Exemple #9
0
 public void Create_WhenCreatingWithMultipleModifier_ShouldGenerateCode()
 {
     Assert.AreEqual("publicabstract", ModifierGenerator.Create(Modifiers.Public, Modifiers.Abstract).ToString());
 }
Exemple #10
0
 public void Create_WhenCreatingWithModifier_ShouldGenerateCode(Modifiers modifier, string expected)
 {
     Assert.AreEqual(expected, ModifierGenerator.Create(modifier).ToString());
 }
Exemple #11
0
 public void TestInitialize()
 {
     _generator = new ModifierGenerator();
 }
Exemple #12
0
 public static IReadOnlyList <ModifierGenerator> AddSet(this IReadOnlyList <ModifierGenerator> orignalList, string modifierFormat, double modifierValue, params string[] modifierNames)
 {
     return(orignalList.Concat(ModifierGenerator.GenerateSet(modifierFormat, modifierValue, modifierNames)).ToArray());
 }