Skip to content

Use Roslyn to load project dependencies and create a semantic model #855

@mcserep

Description

@mcserep

The current approach in the C# plugin to create a complete semantic model of the parsed program is that it is expected that the parsed .NET project was built previously and the build folder is an input folder, and adding all found DLLs as reference to the CSharpCompilation object will load required types for parsing. This includes BCL DLLs and types as well.

This could be significantly simplified. Instead of manually collecting files and DLLs, let Roslyn's MSBuildWorkspace open the .csproj or .sln directly — it resolves references automatically via NuGet/SDK. Minimal working example:

using Microsoft.CodeAnalysis.MSBuild;

var workspace = MSBuildWorkspace.Create();
var solution = await workspace.OpenSolutionAsync("MyApp.sln");

foreach (var project in solution.Projects)
{
    var compilation = await project.GetCompilationAsync();
    foreach (var tree in compilation.SyntaxTrees)
    {
        var model = compilation.GetSemanticModel(tree);
        // execute visitor
    }
}

This requires the Microsoft.CodeAnalysis.Workspaces.MSBuild NuGet package, which we already plan to use in #847 anyway.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    To do

    Status

    To do

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions