using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis; SyntaxTree tree = CSharpSyntaxTree.ParseText("int x = 5;"); CompilationUnitSyntax root = tree.GetCompilationUnitRoot(); VariableDeclarationSyntax varDeclaration = root.DescendantNodes() .OfType() .FirstOrDefault(); var childNodes = varDeclaration.ChildNodes();
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis; SyntaxTree tree = CSharpSyntaxTree.ParseText("if (x == 5) { y = 10; }"); CompilationUnitSyntax root = tree.GetCompilationUnitRoot(); IfStatementSyntax ifStatement = root.DescendantNodes() .OfTypeThis example retrieves the child nodes of the first if statement syntax node in a C# syntax tree. Both of these examples use the Microsoft.CodeAnalysis.CSharp assembly, which is a package library that provides APIs for working with C# source code.() .FirstOrDefault(); var childNodes = ifStatement.ChildNodes();