Roslyn - How can I get all references to a variable in the DiagnosticAnalyzer class?

I am currently trying to define code using Roslyn. I have a variable SyntaxNode

.

I would like to find all references of this variable in my class DiagnosticAnalyzer

. Unfortunately, the method SymbolFinder.FindReferencesAsync

requires a parameter Solution

, and if I know well, I cannot get the current solution from the class DiagnosticAnalyzer

.

How can I get all references to a variable in a class DiagnosticAnalyzer

?

+3


source to share


2 answers


I implemented a similar parser, the algorithm was as follows (it ran for ~ 31ms in a 2000+ doc, but almost always less than 10ms)

  • Iterating through all nodes in SemanticModel.GetRoot()

    and collecting expressions and local decals
  • Get the character of the corresponding identifier
  • Check if it implements IDisposable

    stores in HashSet1

    if it does
  • Iterating through calls where the Dispose method is called
  • Get symbol from Invocation, save to HashSet2

  • Diagnostics report in the first SyntaxReference

    characters found in HashSet1

    but not inHashSet2



This works well, but unfortunately I don't know if there is a more efficient / cleaner way to do this. I can share parts of the code if you like.

+2


source


A similar question with a little scatter

Find expression expressions where method parameter is used



You can help!

-1


source







All Articles