using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; var code = @"using System; namespace Example { public class MyClass { } }"; var tree = CSharpSyntaxTree.ParseText(code); var root = tree.GetRoot(); foreach(var node in root.DescendantNodes().OfType()) { Console.WriteLine(node.Identifier.ValueText); }
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; var code = @"using System; namespace Example { public class MyClass { public void MyMethod() { Console.Write(""Hello World!""); } } }"; var tree = CSharpSyntaxTree.ParseText(code); var root = tree.GetRoot(); var methodCalls = root.DescendantNodes().OfTypeBoth examples utilize the CodeAnalysis package, which is a part of the Roslyn compiler platform.(); foreach(var call in methodCalls) { Console.WriteLine(call.Expression.ToString()); }