string code = "int x = 5;"; SyntaxTree tree = CSharpSyntaxTree.ParseText(code); SyntaxNode node = tree.GetRoot();
SyntaxNode targetNode = ...; IEnumerableIn this example, rootNode is the root SyntaxNode of a syntax tree, and targetNode is the node that we want to find all occurrences of. We use the DescendantNodes method to get all SyntaxNode objects in the tree, and then use the LINQ Where method to filter out only those nodes that are equivalent to the targetNode. The Roslyn compiler platform is the package library that provides the SyntaxNode class and other related classes for program analysis and manipulation.allNodes = rootNode.DescendantNodes(); IEnumerable matchingNodes = allNodes.Where(n => n.IsEquivalentTo(targetNode));