示例#1
0
        public override SyntaxNode VisitVariableDeclaration(VariableDeclarationSyntax node)
        {
            node = (VariableDeclarationSyntax)base.VisitVariableDeclaration(node);

            TypeSyntax type = node.Type;
            SeparatedSyntaxList <VariableDeclaratorSyntax> declarations = node.Variables;

            List <VariableDeclaratorSyntax> listOfVariables = new List <VariableDeclaratorSyntax>();

            List <SyntaxToken> listOfSeperators = new List <SyntaxToken>();

            if (transformKind == TransformKind.DefaultInitAllVars)
            {
                foreach (VariableDeclaratorSyntax decl in declarations)
                {
                    if (decl.Initializer == null)
                    {
                        TypeSyntax newType = type;

                        if (newType.HasLeadingTrivia)
                        {
                            newType = newType.WithLeadingTrivia(new SyntaxTriviaList());
                        }

                        if (newType.HasTrailingTrivia)
                        {
                            newType = newType.WithLeadingTrivia(new SyntaxTriviaList());
                        }

                        SyntaxTrivia            whiteSpaceTrivia = SyntaxFactory.Whitespace(" ");
                        DefaultExpressionSyntax defaultExpr      = SyntaxFactory.DefaultExpression(newType);
                        EqualsValueClauseSyntax equalsClause     = SyntaxFactory.EqualsValueClause(SyntaxFactory.Token(SyntaxFactory.TriviaList(whiteSpaceTrivia), SyntaxKind.EqualsToken, SyntaxFactory.TriviaList(whiteSpaceTrivia)), defaultExpr);

                        VariableDeclaratorSyntax newDecl = SyntaxFactory.VariableDeclarator(decl.Identifier, decl.ArgumentList, equalsClause);
                        listOfVariables.Add(newDecl);
                    }
                    else
                    {
                        listOfVariables.Add(decl);
                    }
                }

                for (int i = 0; i < declarations.SeparatorCount; i++)
                {
                    SyntaxToken seperator = declarations.GetSeparator(i);
                    listOfSeperators.Add(SyntaxFactory.Token(seperator.LeadingTrivia, seperator.Kind(), seperator.TrailingTrivia));
                }

                SeparatedSyntaxList <VariableDeclaratorSyntax> seperatedSyntaxList = SyntaxFactory.SeparatedList(listOfVariables, listOfSeperators);

                return(SyntaxFactory.VariableDeclaration(type, seperatedSyntaxList));
            }

            return(node);
        }
示例#2
0
 private static LocalDeclarationStatementSyntax GenerateObject(TypeSyntax type, SyntaxToken objectIdentifier, ArgumentListSyntax constructorArgs)
 {
     return(LocalDeclarationStatement(
                VariableDeclaration(type)
                .WithVariables(
                    SingletonSeparatedList(
                        VariableDeclarator(objectIdentifier.WithLeadingTrivia(Space))
                        .WithInitializer(
                            EqualsValueClause(
                                ObjectCreationExpression(type.WithLeadingTrivia(Space))
                                .WithArgumentList(constructorArgs)
                                )
                            )
                        )
                    )
                ));
 }