Create a new console application with one line in the Main
method: Console.WriteLine("Hello World")
Remember the path to the .csproj
file and replace it in the example.
Create a new Console Application and install the Microsoft.CodeAnalysis
NuGet package and try the following code:
const string projectPath = @"C:\HelloWorldApplication\HelloWorldProject.csproj";
// Creating a build workspace.
var workspace = MSBuildWorkspace.Create();
// Opening the Hello World project.
var project = workspace.OpenProjectAsync(projectPath).Result;
// Getting the compilation.
var compilation = project.GetCompilationAsync().Result;
foreach (var tree in compilation.SyntaxTrees)
{
Console.WriteLine(tree.FilePath);
var rootSyntaxNode = tree.GetRootAsync().Result;
foreach (var node in rootSyntaxNode.DescendantNodes())
{
Console.WriteLine($" *** {node.Kind()}");
Console.WriteLine($" {node}");
}
}
Console.ReadKey();
This will print all the files and all the syntax nodes in your Hello World project.