7

I'm trying to determine for a class, what does it reference (other namespaces, or external library namespaces). Seems like out of box for a given document/syntaxtree etc, theres no way to do that... and more that I need to just Use symbol finder, iterate through every file in the entire code base, and call find, sticking found references in a map, and then navigating the map backwards.

Am I wrong here? Am I missing something simple? I'm just trying to build a dependency graph... If I start with this class, heres everything that this ultimately needs recursively. I don't mind doing research, but I feel like I'm missing something... and any lead would be helpful

Brief pseudo code concept:

var msWorkspace = MSBuildWorkspace.Create();
var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result;
foreach (var project in solution.Projects)
{
    Append(project.Name);
    foreach (var document in project.Documents)
    {
        Append("\t\t\t" + document.Name);
        if (document.SupportsSemanticModel)
        {
            SemanticModel model = await document.GetSemanticModelAsync();
            var node = await document.GetSyntaxRootAsync();
            //Need to be able to gather dependencies for this current doc 
        }
     }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Ronnyek
  • 482
  • 5
  • 16
  • I added a sample bit of code that shows how I am currently loading documents and getting semantic model – Ronnyek Sep 20 '15 at 21:48

1 Answers1

1

You can do that fairly easily using the semantic model:

node.Descendents()
    .SelectMany(n => semanticModel.GetSymbols(n, workspace, true, new CancellationToken())
    .Distinct()
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • This looks like its definitely the direction I want to go... If I get SyntaxRoot, I can get a SyntaxNode, which has "DescendentNodes". but from my SemanticModel doesnt have a GetSymbols. I was wondering if you could include a bit more in the code sample.. – Ronnyek Sep 20 '15 at 21:31
  • @Ronnyek: Oops; that internal. You can see its source at http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces/Shared/Extensions/SemanticModelExtensions.cs,112. AFAIK, all you need is `GetTypeInfo()` (for types) and `GetSymbolInfo()` (for other members). – SLaks Sep 21 '15 at 00:15
  • The "GetSymbolsEnumerable" you sent does not match the parameters you give in your answer. It also seems to be for finding references, rather then dependencies which are opposite things. Getting dependencies should be faster than getting references. – David Oct 26 '15 at 22:20
  • @DavidKron: No; it is not used for finding references. – SLaks Oct 26 '15 at 22:26
  • My bad, it didnt directly link to the "GetSymbols" function. So you are saying the "GetSymbols" function returns all the ISymbols somehow using the passed syntaxtoken? – David Oct 26 '15 at 22:31
  • Anyway, the ISyntaxFactsService is also internal, so is SpecializedCollections, ISemanticFactsService, GetBestOrAllSymbols()... – David Oct 26 '15 at 22:37
  • @DavidKron: You don't need most of those, since you're running on every node (you can ignore nodes like `await`) – SLaks Oct 26 '15 at 22:45