public void GetTypeForIdentifier()
{
var tree = SyntaxTree.ParseText(@"
class Program
{
public static void Main()
{
var i = 0; i += 1;
}
}");
var mscorlib = MetadataReference.CreateAssemblyReference("mscorlib");
var compilation = Compilation.Create("MyCompilation",
syntaxTrees: new[] { tree }, references: new[] { mscorlib });
var model = compilation.GetSemanticModel(tree);
// Get IdentifierNameSyntax corresponding to the identifier 'var' above.
IdentifierNameSyntax identifier = tree.GetRoot()
.DescendantNodes().OfType<IdentifierNameSyntax>()
.Single(i => i.IsVar);
// Use GetTypeInfo() to get TypeSymbol corresponding to the identifier 'var' above.
TypeSymbol type = model.GetTypeInfo(identifier).Type;
Assert.AreEqual(SpecialType.System_Int32, type.SpecialType);
Assert.AreEqual("int", type.ToDisplayString());
// Alternately, use GetSymbolInfo() to get TypeSymbol corresponding to identifier 'var' above.
type = (TypeSymbol)model.GetSymbolInfo(identifier).Symbol;
Assert.AreEqual(SpecialType.System_Int32, type.SpecialType);
Assert.AreEqual("int", type.ToDisplayString());
}