0

I'm creating na analyzer for Visual Studio, and I need the Solution property required by SymbolFinder.FindSourceDefinitionAsync(ISymbol, Solution) to ensure that I'm really operating over the type I want.

public static async Task<ITypeSymbol> GetBaseScenario(ITypeSymbol type)
    {
        if (type == null)
            return null;

        var origType = await SymbolFinder.FindSourceDefinitionAsync(type, _solution);
        if (BaseScnSymbols.Contains(origType) || BaseVersionScnSymbols.Contains(origType))
            return origType as ITypeSymbol;

        return null;
    }

I can get the Semantic Model, the compilation, but I can't get the solution. How can I get the solution? Is there a better approach for this problem?

Daniel Mendonça
  • 391
  • 1
  • 3
  • 14

2 Answers2

2

You cannot and generally should not try to access the Solution during an analyzer. This is for performance and correctness. This answer still applies. If you describe your actual scenario there's generally a way to not get the Solution.

Community
  • 1
  • 1
Jason Malinowski
  • 18,148
  • 1
  • 38
  • 55
  • Well, in a MethodDeclarationSyntax I'm finding all symbols which have a specific BaseType. I want to extract all hardcoded values associated with those symbols in creationExpression, initializers, assignments and invocations. I was using await SymbolFinder.FindDerivedClassesAsync(namedTS, _solution) to get all the symbols that derive from wanted BaseType, but now I don't have the solution which makes me loop each symbol BaseType.OriginalDefinition to find if the symbol derives or not from it. Was I clear? Do you want me to edit the question? – Daniel Mendonça Apr 04 '17 at 15:46
0

As far as I know the only way to get a solution symbol is to load it via its physical path with MSBuildWorkspace.OpenSolutionAsync.

Tamás Szabó
  • 1,388
  • 11
  • 23
  • Wouldn't that create a mismatch between symbols/syntaxnodes because the object instances are diferent? – Daniel Mendonça Apr 03 '17 at 07:53
  • I'm afraid I don't understand what you mean by that. – Tamás Szabó Apr 03 '17 at 07:58
  • What I'm trying to say is that, if I manually open the solution, it won't be related with the semantic model that I get from the contex in DiagnosticAnalyzer, meaning that SymbolFinder.FindSourceDefinitionAsync() won't match the given symbol with the SourceDefinition. Am I'm wrong? – Daniel Mendonça Apr 03 '17 at 09:18
  • 1
    It seems like there is no straightforward way to do what you want to do. I've found this question: http://stackoverflow.com/questions/23203206/roslyn-current-workspace-in-diagnostic-with-code-fix-project, which is about a similar issue. It is more than 2 years old though, so there might have been some advancements in the topic. – Tamás Szabó Apr 03 '17 at 10:47
  • Guess I'll have to try another approach. Thanks for the help. – Daniel Mendonça Apr 03 '17 at 11:06
  • Sorry we couldn't find a solution :/ Good luck! – Tamás Szabó Apr 03 '17 at 11:07
  • The reason why there's no example for this is that `solution` is not available in a nuget package based analyzer. If you only target VSIX, then you can get a hold of `EnvDTE`, and from that you can get access to the `VisualStudioWorkspace`, which has a reference to the solution. – Tamas Apr 03 '17 at 15:15