/// <summary> /// Check if <paramref name="candidate"/> is <paramref name="symbol"/>. /// Optimized so that the stuff that can be checked in syntax mode is done before calling get symbol. /// </summary> /// <param name="candidate">The <see cref="IdentifierNameSyntax"/>.</param> /// <param name="symbol">The <see cref="QualifiedMethod"/>.</param> /// <param name="semanticModel">The <see cref="SemanticModel"/>.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> /// <returns>True if <paramref name="candidate"/> is <paramref name="symbol"/>.</returns> public static bool IsSymbol(this IdentifierNameSyntax candidate, QualifiedMethod symbol, SemanticModel semanticModel, CancellationToken cancellationToken) { if (candidate is null) { throw new System.ArgumentNullException(nameof(candidate)); } if (symbol is null) { throw new System.ArgumentNullException(nameof(symbol)); } return(candidate.Identifier.ValueText == symbol.Name && semanticModel.TryGetSymbol(candidate, cancellationToken, out var candidateSymbol) && candidateSymbol == symbol); }
/// <summary> /// Try getting the <see cref="IMethodSymbol"/> for the node. /// Gets the semantic model for the tree if the node is not in the tree corresponding to <paramref name="semanticModel"/>. /// </summary> /// <param name="semanticModel">The <see cref="SemanticModel"/>.</param> /// <param name="node">The <see cref="InvocationExpressionSyntax"/>.</param> /// <param name="expected">The expected method.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> /// <param name="symbol">The symbol if found.</param> /// <returns>True if a symbol was found.</returns> public static bool TryGetSymbol(this SemanticModel semanticModel, InvocationExpressionSyntax node, QualifiedMethod expected, CancellationToken cancellationToken, out IMethodSymbol symbol) { symbol = null; if (node == null) { return(false); } if (node.TryGetMethodName(out var name) && name != expected.Name) { return(false); } if (semanticModel.TryGetSymbol(node, cancellationToken, out var candidate) && candidate == expected) { symbol = candidate; return(true); } return(false); }