public static SemanticInfoSummary GetSemanticInfoSummary(this SemanticModel semanticModel, SyntaxNode node) { SemanticInfoSummary summary = new SemanticInfoSummary(); // The information that is available varies by the type of the syntax node. SymbolInfo symbolInfo = SymbolInfo.None; if (node is ExpressionSyntax expr) { symbolInfo = semanticModel.GetSymbolInfo(expr); summary.ConstantValue = semanticModel.GetConstantValue(expr); var typeInfo = semanticModel.GetTypeInfo(expr); summary.Type = (TypeSymbol)typeInfo.Type; summary.ConvertedType = (TypeSymbol)typeInfo.ConvertedType; summary.ImplicitConversion = semanticModel.GetConversion(expr); summary.MemberGroup = semanticModel.GetMemberGroup(expr); } else if (node is AttributeSyntax attribute) { symbolInfo = semanticModel.GetSymbolInfo(attribute); var typeInfo = semanticModel.GetTypeInfo(attribute); summary.Type = (TypeSymbol)typeInfo.Type; summary.ConvertedType = (TypeSymbol)typeInfo.ConvertedType; summary.ImplicitConversion = semanticModel.GetConversion(attribute); summary.MemberGroup = semanticModel.GetMemberGroup(attribute); } else if (node is OrderingSyntax ordering) { symbolInfo = semanticModel.GetSymbolInfo(ordering); } else if (node is SelectOrGroupClauseSyntax selectOrGroupClause) { symbolInfo = semanticModel.GetSymbolInfo(selectOrGroupClause); } else if (node is ConstructorInitializerSyntax initializer) { symbolInfo = semanticModel.GetSymbolInfo(initializer); var typeInfo = semanticModel.GetTypeInfo(initializer); summary.Type = (TypeSymbol)typeInfo.Type; summary.ConvertedType = (TypeSymbol)typeInfo.ConvertedType; summary.ImplicitConversion = semanticModel.GetConversion(initializer); summary.MemberGroup = semanticModel.GetMemberGroup(initializer); } else { throw ExceptionUtilities.UnexpectedValue(node); } summary.Symbol = (Symbol)symbolInfo.Symbol; summary.CandidateReason = symbolInfo.CandidateReason; summary.CandidateSymbols = symbolInfo.CandidateSymbols; if (node is IdentifierNameSyntax identifier) { summary.Alias = semanticModel.GetAliasInfo(identifier); } return(summary); }
public static SemanticInfoSummary GetSpeculativeSemanticInfoSummary( this SemanticModel semanticModel, int position, SyntaxNode node, SpeculativeBindingOption bindingOption ) { SemanticInfoSummary summary = new SemanticInfoSummary(); // The information that is available varies by the type of the syntax node. SymbolInfo symbolInfo = new SymbolInfo(); if (node is ExpressionSyntax) { ExpressionSyntax expr = (ExpressionSyntax)node; symbolInfo = semanticModel.GetSpeculativeSymbolInfo(position, expr, bindingOption); //summary.ConstantValue = semanticModel.GetSpeculativeConstantValue(expr); var typeInfo = semanticModel.GetSpeculativeTypeInfo(position, expr, bindingOption); summary.Type = typeInfo.Type; summary.ConvertedType = typeInfo.ConvertedType; summary.Nullability = typeInfo.Nullability; summary.ConvertedNullability = typeInfo.ConvertedNullability; summary.ImplicitConversion = semanticModel.GetSpeculativeConversion( position, expr, bindingOption ); //summary.MethodGroup = semanticModel.GetSpeculativeMethodGroup(expr); } else if (node is ConstructorInitializerSyntax) { var initializer = (ConstructorInitializerSyntax)node; symbolInfo = semanticModel.GetSpeculativeSymbolInfo(position, initializer); } else { throw new NotSupportedException( "Type of syntax node is not supported by GetSemanticInfoSummary" ); } summary.Symbol = symbolInfo.Symbol; summary.CandidateReason = symbolInfo.CandidateReason; summary.CandidateSymbols = symbolInfo.CandidateSymbols; if (node is IdentifierNameSyntax) { summary.Alias = semanticModel.GetSpeculativeAliasInfo( position, (IdentifierNameSyntax)node, bindingOption ); } return(summary); }