MSBuildLocator.RegisterDefaults();
var workspace = MSBuildWorkspace.Create();
workspace.LoadMetadataForReferencedProjects = true;
var solution = workspace.OpenSolutionAsync("C:\\Users\\Dell\\Desktop\\TestCompilation\\RoslynDetectingApproach\\RoslynDetectingApproach.sln").Result;
foreach (var project in solution.Projects)
{
var compilation = await project.GetCompilationAsync();
var metadata = compilation.ExternalReferences;
foreach (var item in metadata)
{
System.Console.WriteLine(item.Display);
}
}
I tried this code above to get all external references but it doesn't include packages and assemblies referenced. It display only project references. For example for a project that have many references types:
<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="1.2.6" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.6.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Test\Test.csproj" />
</ItemGroup>
My code can't get packages referenced it get only the projects referenced and in this example it will be the project Test.Can you please give any suggestions to get referenced packages and assemblies.