获取 ReferenceLocation 的符号 [英] Get Symbol for ReferenceLocation
问题描述
我正在使用 SymbolFinder
在我的解决方案中查找对某个类型的所有引用,如下所示:
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?
}
这部分工作正常,SymbolFinder
返回正确的 ReferenceLocations
(手动检查).我实际上对这些位置的符号感兴趣,以获取有关引用的更多(语义)信息,因此我可以对其进行过滤/使用它(例如,仅适用于属性).
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).
似乎还没有关于 Roslyn 的公开信息,而且我在 SDK 预览版的示例中找不到任何工作与 SymbolFinder
的结果相关的内容.所以这是我的问题:是否有可能获得与 ReferenceLocation
对应的符号?怎么样?
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?
推荐答案
因此,在任何这些位置都没有严格的符号",至少没有与生俱来的概念.您可以做的是获取该位置,然后找到封闭符号.您可以获取位置的 SyntaxTree
并获得一个 Document
.从那里,调用 GetSemanticModelAsync
,然后调用 ISemanticModel.GetEnclosureSymbol
.
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
.
例如,这里有一些(内部)代码为 FAR 本身执行此操作:https://github.com/dotnet/roslyn/blob/748d6ab1b504ceee0c29f132fdcbe2a777aa88ea/src/Workspaces/Core/Portable/6Rem/a>
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
这篇关于获取 ReferenceLocation 的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!