Learn Roslyn Now: Part 15 The SymbolVisitor

I had a question the other day that I ended up taking directly to the Roslyn issues: How do I get a list of all of the types available to a compilation? Schabse Laks (@Schabse) and David Glick (@daveaglick) introduced me to a cool class I hadn’t encountered before: The SymbolVisitor.

In previous posts we touched on the CSharpSyntaxWalker and the CSharpSyntaxRewriter. The SymbolVisitor is the analogue of SyntaxVisitor, but applies at the symbol level. Unfortunately unlike the SyntaxWalker and CSharpSyntaxRewriter, when using the SymbolVisitor we must construct the scaffolding code to visit all the nodes.

To simply list all the types available to a compilation we can use the following.

In order to visit all the methods available to a given compilation we can use the following:

It’s important to be aware of how you must structure your code in order to visit all the symbols you’re interested in. By now you may have noticed that using this API directly makes me a little sad. If I’m interested in visiting method symbols, I don’t want to have to write code that visits namespaces and types.

Hopefully at some point we’ll get a SymbolWalker class that we can use to separate out our implemenation from the traversal code. I’ve opened an issue on Roslyn requesting this feature. (It seems like it’s going to be challenging to implement and would require working with both syntax and symbols).

Finding All Named Type Symbols

Finally, you might be wondering how I answered my original question: How do we get a list of all of the types available to a compilation? My implementation is below:

I should note that after implementing this solution, I came to the conclusion that it was too slow for our purposes. We got a major performance boost by only visiting symbols within namespaces defined within source, but it was still about an order of magnitude slower than the simply searching for types via the SymbolFinder class.

Still, the SymbolVisitor class is probably appropriate for one-off uses during compilation or for visiting a subset of available symbols. At the very least, it’s worth being aware of.

10 thoughts on “Learn Roslyn Now: Part 15 The SymbolVisitor

  1. I am regularly reading and enjoying your posts. However, using feedly, I have to go to your site every time, as feedly happens to never include the source code hosted with ❤ by GitHub. I first thought that this was feedly's fault. It isn't, as it is basing its rendering on the RSS feed which does not have the source code… Any idea why it is not showing up in the RSS feed (https://joshvarty.wordpress.com/category/uncategorized/feed/)? Is something wrong with the WordPress plug-in you are using? Is this by design?

    1. It’s not by design, unfortunately it doesn’t seem like WordPress is embedding Gists in a nice way. 😦

      I’ll try to figure out a workaround or maybe I’ll end up moving my code snippets to another service.

    1. Nope, as far as I know you can only get the semantic model from a compilation or from a Document. Then you can use the semantic model to get symbols from corresponding pieces of syntax.

  2. Thanks, Josh. I appreciate the time you took to write these priceless posts. Since your series has turned to a Roslyn reference for many, I think you need to keep your content relevant and updated. For example, I think mentioning the discussion you had on Github around SymbolWalker could be enlightening about the graph structure of Symbols.

  3. It’s been a while since your original post and I’m curious to know if you have found the speed is still an issue between symbol finder and the visitor pattern. In particular I’m looking to make graphs out of sections of the code that may span an entire solution.

    1. It’s been a long time since I’ve used it so I can’t say for sure. I think it depends on how frequently you need to visit symbols in your compilation. If I remember correctly we were considering using it fairly frequently while a user was typing, which ended up being a problem.

      If you’re hoping to look at all the symbols once (or less frequently than we were) the performance may not be an issue for you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s