2

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?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • What exactly is your project? – SLaks Aug 02 '17 at 16:50
  • My project's objective is to extract information about certain method's invocations and then generate json files with that information, so that it can be displayed on the project's documentation web portal. – Renato Campos Aug 03 '17 at 07:20
  • Where does `Config.SolutionPath` come from? Does it have the correct value? Oh and by the way, when something *clearly* is the problem, it probably isn't. – Kris Vandermotten Aug 03 '17 at 22:50
  • Config.SolutionPath has the correct value, it is a string that comes from another class. If the solution path was not correct, the solution would not even load, which is not the case. – Renato Campos Aug 04 '17 at 15:23
  • see my answer on how to grab out the current solution from the analyzer https://stackoverflow.com/a/44162871/1938988 – johnny 5 Aug 10 '17 at 16:52

1 Answers1

0

You are using MSBuildWorkspace to open a solution. Typically, when use of MSBuildWorkspace leads to projects not being loaded correctly, no source files, etc, there has been a failure during msbuild processing. This usually happens when your application does not have the same binding redirects in its app.config file that msbuild uses (in msbuild.exe.config), which causes some custom tasks/extensions to fail to load due to versioning mismatch.

You'll want to copy the <assemblyBinding> section into your app.config.

Matt Warren
  • 1,956
  • 14
  • 15