I was trying to write an analyser to get information about some methods using the roslyn syntax tree. The problem is: The analyser that I am writing, needs to be in the same solution as the solution that I want to analyse. So, this is my code:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public static class Main
{
public static Solution solution { get; set; } = null;
public static string GetMethodInfo(string methodToFind)
{
Task<Solution> GetSolutionTask = null;
string namespaceToFind, classToFind, methodToFind, invocationToFind;
if (solution == null)
{
var workspace = Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create();
GetSolutionTask = workspace.OpenSolutionAsync(Config.SolutionPath);
}
if (GetSolutionTask != null) solution = GetSolutionTask.Result;
foreach (Project proj in solution.Projects)
{
Compilation compilation = proj.GetCompilationAsync().Result;
foreach (var tree in compilation.SyntaxTrees)
{
findMethodAndProcessIt()...
}
}
return String.Empty;
}
}
The problem I get is that no compilation has any syntax tree. I tried this same code by opening other solutions and it works. So clearly the problem here is to be trying to open the solution that the visual studio is using. I have already tried to run this code with visual studio closed, only running the .exe , but the problem persists. Do you have any idea on how to solve this?