public void GetTypeForExpressions()
{
var source = @"
using System;
class Program
{
public void M(short[] s)
{
var d = 1.0;
Console.WriteLine(s[0] + d);
}
public static void Main()
{
}
}";
var solutionId = SolutionId.CreateNewId();
ProjectId projectId;
DocumentId documentId;
var solution = Solution.Create(solutionId)
.AddCSharpProject("MyProject", "MyProject", out projectId)
.AddMetadataReference(projectId, MetadataReference.CreateAssemblyReference("mscorlib"))
.AddDocument(projectId, "MyFile.cs", source, out documentId);
var document = solution.GetDocument(documentId);
var model = (SemanticModel)document.GetSemanticModel();
// Get BinaryExpressionSyntax corresponding to the expression 's[0] + d' above.
BinaryExpressionSyntax addExpression = document.GetSyntaxRoot()
.DescendantNodes().OfType<BinaryExpressionSyntax>().Single();
// Get TypeSymbol corresponding to expression 's[0] + d' above.
TypeInfo expressionTypeInfo = model.GetTypeInfo(addExpression);
TypeSymbol expressionType = expressionTypeInfo.Type;
Assert.AreEqual(SpecialType.System_Double, expressionType.SpecialType);
Assert.AreEqual("double", expressionType.ToDisplayString());
Assert.AreEqual(SpecialType.System_Double, expressionTypeInfo.ConvertedType.SpecialType);
Assert.IsTrue(expressionTypeInfo.ImplicitConversion.IsIdentity);
// Get IdentifierNameSyntax corresponding to the variable 'd' in expression 's[0] + d' above.
var identifier = (IdentifierNameSyntax)addExpression.Right;
// Use GetTypeInfo() to get TypeSymbol corresponding to variable 'd' above.
TypeInfo variableTypeInfo = model.GetTypeInfo(identifier);
TypeSymbol variableType = variableTypeInfo.Type;
Assert.AreEqual(SpecialType.System_Double, variableType.SpecialType);
Assert.AreEqual("double", variableType.ToDisplayString());
Assert.AreEqual(SpecialType.System_Double, variableTypeInfo.ConvertedType.SpecialType);
Assert.IsTrue(variableTypeInfo.ImplicitConversion.IsIdentity);
// Alternately, use GetSymbolInfo() to get TypeSymbol corresponding to variable 'd' above.
variableType = ((LocalSymbol)model.GetSymbolInfo(identifier).Symbol).Type;
Assert.AreEqual(SpecialType.System_Double, variableType.SpecialType);
Assert.AreEqual("double", variableType.ToDisplayString());
// Get ElementAccessExpressionSyntax corresponding to 's[0]' in expression 's[0] + d' above.
var elementAccess = (ElementAccessExpressionSyntax)addExpression.Left;
// Use GetTypeInfo() to get TypeSymbol corresponding to 's[0]' above.
expressionTypeInfo = model.GetTypeInfo(elementAccess);
expressionType = expressionTypeInfo.Type;
Assert.AreEqual(SpecialType.System_Int16, expressionType.SpecialType);
Assert.AreEqual("short", expressionType.ToDisplayString());
Assert.AreEqual(SpecialType.System_Double, expressionTypeInfo.ConvertedType.SpecialType);
Assert.IsTrue(expressionTypeInfo.ImplicitConversion.IsImplicit && expressionTypeInfo.ImplicitConversion.IsNumeric);
// Get IdentifierNameSyntax corresponding to the parameter 's' in expression 's[0] + d' above.
identifier = (IdentifierNameSyntax)elementAccess.Expression;
// Use GetTypeInfo() to get TypeSymbol corresponding to parameter 's' above.
variableTypeInfo = model.GetTypeInfo(identifier);
variableType = variableTypeInfo.Type;
Assert.AreEqual("short[]", variableType.ToDisplayString());
Assert.AreEqual("short[]", variableTypeInfo.ConvertedType.ToDisplayString());
Assert.IsTrue(variableTypeInfo.ImplicitConversion.IsIdentity);
// Alternately, use GetSymbolInfo() to get TypeSymbol corresponding to parameter 's' above.
variableType = ((ParameterSymbol)model.GetSymbolInfo(identifier).Symbol).Type;
Assert.AreEqual("short[]", variableType.ToDisplayString());
Assert.AreEqual(SpecialType.System_Int16, ((ArrayTypeSymbol)variableType).ElementType.SpecialType);
}