Roslyn: current workspace in diagnostics with a code fix project

How can I get information about the current workspace (for example, the project path, the solution path) in Diagnostics with the project code correction?

I am doing diagnostics like ISyntaxNodeAnalyzer

I need to access SymbolFinder.FindImplementationsAsync, but for this I need an instance of the solution

EDIT: I have a code:

var syntax = (LocalDeclarationStatementSyntax) node;
var type = syntax.Declaration.Type;
var typeSymbol = semanticModel.GetTypeInfo(type).ConvertedType;

I would like to know all the usages / links of type Symbol. TypeSymbol represents a class located in the source code.

To do this, I wanted to use SymbolFinder, but SymbolFinder methods require an instance of a solution ... In the old version of Roslyn, the document was set as a parameter of the diagnostic parameter, you could get a project and a solution.

+4
2

, , . , .

, - , , . , ICompilationStartedAnalyzer, ICompilationEndedAnalyzer.

+6

, , gaurentee, ..... Windows.

public static class RoslynExtensions
{
    public static Solution GetSolution(this SyntaxNodeAnalysisContext context)
    {
        var workspace = context.Options.GetPrivatePropertyValue<object>("Workspace");
        return workspace.GetPrivatePropertyValue<Solution>("CurrentSolution");
    }

    public static T GetPrivatePropertyValue<T>(this object obj, string propName)
    {
        if (obj == null)
        {
            throw new ArgumentNullException(nameof(obj));
        }

        var pi = obj.GetType().GetRuntimeProperty(propName);

        if (pi == null)
        {
            throw new ArgumentOutOfRangeException(nameof(propName), $"Property {propName} was not found in Type {obj.GetType().FullName}");
        }

        return (T)pi.GetValue(obj, null);
    }
}

:

public override void Initialize(AnalysisContext context)
{
    context.RegisterSyntaxNodeAction(AnalyzeConstDeclaration, SyntaxKind.FieldDeclaration);
}

public static void AnalyzeConstDeclaration(SyntaxNodeAnalysisContext context)
{
     var solution = context.GetSolution();
}
+2

Source: https://habr.com/ru/post/1537402/


All Articles