Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Symbol for ReferenceLocation

Tags:

c#

roslyn

I'm using the SymbolFinder to find all references to a certain type in my solution like this:

ISymbol typeOfInterest = compilation.GetTypeByMetadataName(
    "System.Reflection.PropertyInfo");
var references = SymbolFinder.FindReferencesAsync(typeOfInterest, solution).Result;
foreach (var reference in references)
{
    // reference.Locations => symbol?
}

This part is working fine, the SymbolFinder returns correct ReferenceLocations (upon manual inspection). I'm actually interested in the symbols at these locations to get more (semantic) information about the references, so I can filter upon / work with it (e.g. only work on properties).

There seems to be very little public information on Roslyn yet and I couldn't find anything working with the results of SymbolFinder in the samples of the SDK Preview. So here is my question: Is it possible to get the symbol corresponding to a ReferenceLocation? How?

like image 284
andyp Avatar asked Apr 07 '14 22:04

andyp


1 Answers

So, there isn't strictly a "symbol" at any of these locations, at least no innate concept of that. What you can do is take that Location, and find the enclosing symbol. You can take the location's SyntaxTree and get a Document. From there, call GetSemanticModelAsync, and then call ISemanticModel.GetEnclosingSymbol.

As an example, here's some (internal) code that does this for FAR itself: https://github.com/dotnet/roslyn/blob/748d6ab1b504ceee0c29f132fdcbe2a777aa88ea/src/Workspaces/Core/Portable/FindSymbols/ReferenceLocationExtensions.cs#L67-L101

like image 192
Jason Malinowski Avatar answered Nov 08 '22 05:11

Jason Malinowski