In this recipe, we wrote a C# console application based on Roslyn Compiler API to parse and transform source text. As mentioned earlier, our application demonstrates three core syntax operations on the parsed tree: edit, add, and remove. Let's walk through the code and understand how we implemented these operations:
public static void Main(string[] args){ // Parse arguments to get source file. var filePath = ParseArguments(args); if (filePath == null) { return; } // Parse text into SyntaxTree. var tree = Parse(filePath); var root = (CompilationUnitSyntax)tree.GetRoot(); // Transform syntax tree to edit/add/remove syntax. root = EditClassDeclarations(root); root = AddDocCommentsToClassDeclarations(root); root = RemoveEmptyClassDeclarations(root); ...