string code = "class MyClass { void Method1() { } int Method2(int a, int b) { return a + b; } }"; SyntaxTree tree = CSharpSyntaxTree.ParseText(code); SyntaxNode root = tree.GetRoot(); var methods = root.DescendantNodes().OfType(); foreach (var method in methods) { Console.WriteLine(method.Identifier); }
SyntaxNode node = ...; // some syntax node if (node.IsKind(SyntaxKind.BinaryExpression)) { Console.WriteLine("This is a binary expression"); } else { Console.WriteLine("This is not a binary expression"); }In this example, we have a syntax node (represented by the variable `node`) and we want to check if it represents a binary operator (like `+`, `-`, `*`, `/`, etc.). We use the IsKind() method and pass the appropriate enumeration value `SyntaxKind.BinaryExpression`. Both of the above examples make use of the Microsoft.CodeAnalysis.CSharp Namespace.