/// <summary> /// Check if the attribute is of the expected type. /// </summary> /// <param name="attribute">The <see cref="AttributeSyntax"/>.</param> /// <param name="expected">The expected type.</param> /// <param name="semanticModel">The <see cref="SemanticModel"/>.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> /// <returns>True if the attribute is of the expected type.</returns> public static bool IsType(AttributeSyntax attribute, QualifiedType expected, SemanticModel semanticModel, CancellationToken cancellationToken) { if (attribute == null) { return(false); } if (attribute.Name is SimpleNameSyntax simpleName) { if (!IsMatch(simpleName, expected) && !AliasWalker.TryGet(attribute.SyntaxTree, simpleName.Identifier.ValueText, out _)) { return(false); } } else if (attribute.Name is QualifiedNameSyntax qualifiedName && qualifiedName.Right is SimpleNameSyntax typeName) { if (!IsMatch(typeName, expected) && !AliasWalker.TryGet(attribute.SyntaxTree, typeName.Identifier.ValueText, out _)) { return(false); } } return(semanticModel.TryGetType(attribute, cancellationToken, out var attributeType) && attributeType == expected); bool IsMatch(SimpleNameSyntax sn, QualifiedType qt) { return(sn.Identifier.ValueText == qt.Type || qt.Type.IsParts(sn.Identifier.ValueText, "Attribute")); } }
/// <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="ObjectCreationExpressionSyntax"/>.</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, ObjectCreationExpressionSyntax node, QualifiedType expected, CancellationToken cancellationToken, out IMethodSymbol symbol) { if (node.Type is SimpleNameSyntax typeName && (typeName.Identifier.ValueText == expected.Type || AliasWalker.TryGet(node.SyntaxTree, typeName.Identifier.ValueText, out _))) { symbol = semanticModel.GetSymbolSafe(node, cancellationToken); return(symbol?.ContainingType == expected); } if (node.Type is QualifiedNameSyntax qualifiedName && qualifiedName.Right.Identifier.ValueText == expected.Type) { symbol = semanticModel.GetSymbolSafe(node, cancellationToken); return(symbol?.ContainingType == expected); } symbol = null; return(false); }
/// <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="ObjectCreationExpressionSyntax"/>.</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, ObjectCreationExpressionSyntax node, QualifiedType expected, CancellationToken cancellationToken, [NotNullWhen(true)] out IMethodSymbol?symbol) { if (semanticModel is null) { throw new System.ArgumentNullException(nameof(semanticModel)); } if (expected is null) { throw new System.ArgumentNullException(nameof(expected)); } if (node is null) { throw new System.ArgumentNullException(nameof(node)); } if (node.Type is SimpleNameSyntax typeName && (typeName.Identifier.ValueText == expected.Type || AliasWalker.TryGet(node.SyntaxTree, typeName.Identifier.ValueText, out _))) { symbol = semanticModel.GetSymbolSafe(node, cancellationToken); return(symbol is { } &&