Daniel PP Cabral
Daniel PP Cabral

Reputation: 1624

Roslyn : How to get unresolved types

I am using Roslyn's September 2012 CTP.

What is the most elegant way to get unresolved types in a c# code document? Eg. Type Guid requires the System namespace. Currently I have something like this:

            var semanticModel = (SemanticModel)document.GetSemanticModel();
            var tree = (SyntaxTree)document.GetSyntaxTree();

            //get unresolved types
            var unresolvedTypes = tree.GetRoot().DescendantNodes().OfType<IdentifierNameSyntax>()
                .Where(x => semanticModel.GetSymbolInfo(x).Symbol == null);

Is it correct to use IdentifierNameSyntax and GetSymbolInfo?

Also what is the difference between GetSymbolInfo and GetTypeInfo, they both look very similar to me.

Upvotes: 5

Views: 1680

Answers (1)

Kevin Pilch
Kevin Pilch

Reputation: 11615

There are several questions here.

Q: Is it correct to use IdentifierNameSyntax?
A: You probably want to use SimpleNameSyntax to handle resolving generic types. Also, you may not want to look at ALL SimpleNameSyntax elements. You will get false positives for things that are not actually in a type context (for example, imagine some code like var x = Console();

Q: Is it correct to use GetSymbolInfo and check for null?
A: Yes, this is the right thing to check here.

Q: What is the difference between GetSymbolInfo and GetTypeInfo?
A: For a syntax that represents a type name, there is no difference. However, for arbitrary expressions GetSymbolInfo represents the specific symbol of the expression (for example, method call, indexer access, array access, overloaded operator, etc), and GetTypeInfo represents the resulting type (so that you would know what type to generate if you were adding a declaration for the expression). Take for example the InvocationExpressionSyntax for "myString.GetHashCode()". GetSymbolInfo would return the method symbol for GetHashCode(), while GetTypeInfo would return System.Int32.

Upvotes: 9

Related Questions