0

I want to find all the references of a private member of a class. I've tried to do this:

MemberInfo member = ...//the private member for which I want to find its references 
Type type = member.DeclaringType;
string assemblyName = type.Assembly.GetName().Name;
Solution solution = workspace.CurrentSolution;
Project project = solution.Projects.First(x => x.AssemblyName == assemblyName);
Compilation compilation = project.GetCompilation();
ClassDeclarationSyntax classDeclaration = compilation.GetClassDeclaration(type);
MemberDeclarationSyntax memberDeclaration = classDeclaration.GetMemberDeclaration(member.Name);
SemanticModel semanticModel = compilation.GetSemanticModel(classDeclaration.SyntaxTree);
ISymbol memberSymbol = semanticModel.GetSymbolInfo(memberDeclaration).Symbol; ==> this is null since GetSymbolInfo does not expect a MemberDeclaationSyntax
IEnumerable<ReferencedSymbol> references = SymbolFinder.FindReferencesAsync(memberSymbol, solution).Result;

How can I find all the references of the private member?

Tamas Ionut
  • 4,240
  • 5
  • 36
  • 59

1 Answers1

0

Because MemberDeclarationSyntax is a SyntaxNode, you need to use the Semantic.GetDeclaredSymbol method to get the symbol associated with this node. For example:

var memberDeclarationSyntax = ( MemberDeclarationSyntax ) root.FindNode( diagnostic.Location.SourceSpan );
var declaredSymbol = semanticModel.GetDeclaredSymbol( memberDeclarationSyntax );
var references = await SymbolFinder
    .FindReferencesAsync( declaredSymbol , context.Document.Project.Solution )
    .ConfigureAwait( false );
Sievajet
  • 3,443
  • 2
  • 18
  • 22