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.
There are several questions here.
Q: Is it correct to use
IdentifierNameSyntax?A: You probably want to use
SimpleNameSyntaxto handle resolving generic types. Also, you may not want to look at ALLSimpleNameSyntaxelements. You will get false positives for things that are not actually in a type context (for example, imagine some code likevar x = Console();Q: Is it correct to use
GetSymbolInfoand check for null?A: Yes, this is the right thing to check here.
Q: What is the difference between
GetSymbolInfoandGetTypeInfo?A: For a syntax that represents a type name, there is no difference. However, for arbitrary expressions
GetSymbolInforepresents the specific symbol of the expression (for example, method call, indexer access, array access, overloaded operator, etc), andGetTypeInforepresents the resulting type (so that you would know what type to generate if you were adding a declaration for the expression). Take for example theInvocationExpressionSyntaxfor “myString.GetHashCode()“.GetSymbolInfowould return the method symbol forGetHashCode(), whileGetTypeInfowould returnSystem.Int32.