2

Using the Roslyn SDK, which SyntaxNode (derived) types can be bound to Symbols, and which methods are used to bind them?

So far, I am aware of SemanticModel.GetSymbolInfo(), SemanticModel.GetDeclaredSymbol() and SemanticModel.GetTypeInfo(). From my understanding, GetDeclaredSymbol() is used for binding declarations and GetTypeInfo() for binding expressions. I don't really know what GetSymbolInfo() is for specifically, I have been using it as a fallback if the other two produce null. Are there any additional methods for binding?

Also, is there a list of which SyntaxNode maps to which Symbol? Or at least a list of the SyntaxNodes that cannot be bound at all? Currently I am figuring out which SyntaxNode types to use for binding certain code elements by trial and error.

1 Answer 1

3

You are correct that SemanticModel.GetDeclaredSymbol() returns symbol metadata for declarations. However, SemanticModel.GetTypeInfo() returns type information about the expression as the MSDN documentations says.

The SemanticModel.GetSymbolInfo() returns what symbol, if any, the given expression syntax bound to in the program. Again, I'm only referring to the docs here. But now let me illustrate the difference.

Suppose that you have some code which looks like this:

public string Foo() => "Foo";


//...

var s = Foo();

Suppose you are calling GetTypeInfo and GetSymbolInfo for the Foo() syntax node. Then GetTypeInfo should return you the type of the expression Foo() which means it should basically return a type symbol for string.

At the same time, GetSymbolInfo returns you information about the actual symbol bound to the expression. Since this is a call to Foo method it should return you a IMethodSymbol representing Foo method and its metadata.

The usage scenarios can be formulated like this:

  • If you need metadata about a symbol bound to the expression node, then you should use GetSymbolInfo.
  • If you need metadata about a symbol bound to a declaration node, then you should use GetDeclaredSymbol.
  • If you need to get a type symbol representing the type returned by the expression node (the type of the expression), then you should use GetTypeInfo.
Sign up to request clarification or add additional context in comments.

10 Comments

One way to think about GetTypoInfo is thinking of Visual Studio's IntelliSense. If you have some expression and you type '.'; GetTypeInfo tells you what the type is that we should show the members of.
Thanks, so they each bind different aspects of the SyntaxNode. But how to know which SyntaxNode you have to provide to each of them to get that information? There's a number of options here including: VariableDeclarationSyntax: Fails to bind with all 3 methods. (Expected GetDeclaredSymbol() to work for this, since the syntax assings a name to an expression). IdentifierNameSyntax: works as expected from your description. EqualsValueClauseSyntax (from my understanding the assignment expression): Fails to bind with all 3 (expected GetTypeInfo to work since its an expression)
Try using SyntaxVisualizer tool in VS: View -> Other Windows -> Syntax Visualizer. learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/…
Then, in Syntax Visualizer when you find the AST node you wish to use, you need to select it in the tree and right click on it to open context menu. In the menu you will see several items. You should use "View Symbol (if any)", that's the most useful among them. If there is a bound symbol, Syntax Visualizer will display its info.
Thanks that seems very useful, but im using Jetbrains Rider, which comes with a syntax tree visualizer but unfortunately seems to lack the ability to link the syntax node to their related symbols.
That's a pity. It seems that Roslyn support in Rider does not have some of the most useful features. In that case you can try and see which nodes can be bound to symbols. Maybe you can write a small console app that will use Roslyn to parse some strings, and then try getting symbols from them. Then you can try different code fragments to check which nodes can be bound.
If that feature is available in VS Community I'll consider installing it just for this if it gets too bad.
AFAIK, it should be available.
I believe GetSymbolInfo is very niche and won't be very useful to most developers. If you use GetTypeInfo(...).Type on an attribute syntax, you will get information about the attribute type, whereas GetSymbolInfo will give you a symbol that corresponds to the attribute constructor (the method) instead.
Depends on the analysis you do. I constantly use GetSymbolInfo for a lot of things like custom syntax coloring and analysis. At the same time, I rarely need to use GetTypeInfo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.