how do I actually reference roslyn code analysis for runtime?

68 Views Asked by At

So I want to do some things which require inspecting my solution files and projects. Roslyn seems like the way to go - so I find some sample code:

  MSBuildLocator.RegisterDefaults();
  var workspace = MSBuildWorkspace.Create();
  var sln = await workspace.OpenSolutionAsync(solutionPath);

put the following in csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="7.0.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.7.0" />
    <PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
</ItemGroup>

and press run - it builds fine - and then doesn't work at all -

Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp.Workspaces, Version=4.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified

and google as I might, I can't find a solution to it - I found something that said yeah, just add the msbuild locator, which did nothing.

Any ideas how I fix this?

1

There are 1 best solutions below

0
Darren Oakey On

I sort of got it working by

    <PackageReference Include="Microsoft.CodeAnalysis.Csharp.Workspaces" Version="4.7.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.7.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Features" Version="4.7.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Csharp.Features" Version="4.7.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Features" Version="4.7.0" />
    <PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.7.0" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />

and oddly enough it works - but in another thread - it's throwing an unhandled exception

'Could not load file or assembly 'C:\Program Files\dotnet\sdk\6.0.412\Microsoft.Build.resources.dll'. The system cannot find the file specified.'