示例#1
0
            public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
            {
                // If this is the declaration statement of inlined variable
                if (node.Equals(this.localDeclarationStatementSyntax))
                {
                    // If the declaration statement contains only one declarator (with inlined variable)
                    // it can be simply removed
                    // Considers:
                    // { int a = 1; } -> {}
                    if (node.Declaration.Variables.Count == 1)
                    {
                        return(null);
                    }
                    else
                    {
                        // Process declaration children (possibly replace identifiers in other declarators)
                        // Considers:
                        // int a = 2, b = a; -> int b = 2;
                        LocalDeclarationStatementSyntax newDeclaration = (LocalDeclarationStatementSyntax)base.VisitLocalDeclarationStatement(node);

                        // Now that I have processed children, I can process the declaration itself by removing the unneeded declarator

                        // Get the inlined variable declarator from new declaration statement
                        VariableDeclaratorSyntax newInlinedVariableDeclarator = newDeclaration.Declaration.Variables
                                                                                .Where(n => n.Identifier.ValueText == this.variableDeclarator.Identifier.ValueText)
                                                                                .Single();

                        // Remove declarator from the declaration
                        newDeclaration = newDeclaration.ReplaceNode(newInlinedVariableDeclarator, null);

                        return(newDeclaration);
                    }
                }
                else
                {
                    return(base.VisitLocalDeclarationStatement(node));
                }
            }