using Microsoft.CodeAnalysis.CSharp.Syntax; // Create a syntax node var node = SyntaxFactory.ParseExpression("1 + 2"); // Get the ancestors of the node var ancestors = node.Ancestors(); // Iterate over the ancestors and do some processing foreach (var ancestor in ancestors) { // Do something with the ancestor node }
using Microsoft.CodeAnalysis.CSharp.Syntax; // Create a syntax node var node = SyntaxFactory.ParseExpression("1 + 2"); // Find the parent of the node var parent = node.Ancestors().FirstOrDefault(x => x is BinaryExpressionSyntax); // Do something with the parent nodeIn both examples, we are using the ExpressionSyntax class, which is part of the Microsoft.CodeAnalysis.CSharp.Syntax package library.