Skip to content

Instantly share code, notes, and snippets.

@cezarypiatek
Created January 24, 2020 20:01
Show Gist options
  • Save cezarypiatek/1b20d8e64d42cc9ea21fe27a8041e610 to your computer and use it in GitHub Desktop.
Save cezarypiatek/1b20d8e64d42cc9ea21fe27a8041e610 to your computer and use it in GitHub Desktop.
private static ITypeSymbol GetRangeVariableType(SemanticModel semanticModel, IRangeVariableSymbol symbol)
{
ITypeSymbol type = null;
if (!symbol.Locations.IsEmpty)
{
var location = symbol.Locations.First();
if (location.IsInSource && location.SourceTree == semanticModel.SyntaxTree)
{
var token = location.SourceTree.GetRoot().FindToken(symbol.Locations.First().SourceSpan.Start);
var queryBody = GetQueryBody(token);
if (queryBody != null)
{
// To heuristically determine the type of the range variable in a query
// clause, we speculatively bind the name of the variable in the select
// or group clause of the query body.
var identifierName = SyntaxFactory.IdentifierName(symbol.Name);
type = semanticModel.GetSpeculativeTypeInfo(
queryBody.SelectOrGroup.Span.End - 1, identifierName, SpeculativeBindingOption.BindAsExpression).Type;
}
var identifier = token.Parent as IdentifierNameSyntax;
if (identifier != null)
{
type = semanticModel.GetTypeInfo(identifier).Type;
}
}
}
return type;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment