internal static bool TryMatchTypeGetType(InvocationExpressionSyntax invocation, SyntaxNodeAnalysisContext context, out TypeNameArgument typeName, out ArgumentAndValue <bool> ignoreCase) { if (invocation.TryGetTarget(KnownSymbol.Type.GetType, context.SemanticModel, context.CancellationToken, out var target) && target.TryFindParameter("typeName", out var nameParameter) && invocation.TryFindArgument(nameParameter, out var nameArg) && nameArg.TryGetStringValue(context.SemanticModel, context.CancellationToken, out string name)) { typeName = new TypeNameArgument(nameArg, name); switch (target.Parameters.Length) { case 1: ignoreCase = default(ArgumentAndValue <bool>); return(true); case 2 when target.TryFindParameter("throwOnError", out _): ignoreCase = default(ArgumentAndValue <bool>); return(true); case 3 when target.TryFindParameter("throwOnError", out _) && target.TryFindParameter("ignoreCase", out var ignoreCaseParameter) && invocation.TryFindArgument(ignoreCaseParameter, out var ignoreCaseArg) && context.SemanticModel.TryGetConstantValue(ignoreCaseArg.Expression, context.CancellationToken, out bool ignoreNameCase): ignoreCase = new ArgumentAndValue <bool>(ignoreCaseArg, ignoreNameCase); return(true); } } typeName = default(TypeNameArgument); ignoreCase = default(ArgumentAndValue <bool>); return(false); }
private static bool TryGetArgs(InvocationExpressionSyntax invocation, SyntaxNodeAnalysisContext context, out IMethodSymbol target, out ExpressionSyntax member, out ArgumentAndValue <ITypeSymbol> attributeType, out ArgumentSyntax inheritsArg) { if ((invocation.TryGetTarget(KnownSymbol.Attribute.IsDefined, context.SemanticModel, context.CancellationToken, out target) || invocation.TryGetTarget(KnownSymbol.CustomAttributeExtensions.IsDefined, context.SemanticModel, context.CancellationToken, out target)) && target.TryFindParameter("attributeType", out var attributeTypeParameter) && invocation.TryFindArgument(attributeTypeParameter, out var attributeTypeArg) && attributeTypeArg.Expression is TypeOfExpressionSyntax typeOf && context.SemanticModel.TryGetType(typeOf.Type, context.CancellationToken, out var typeSymbol)) { attributeType = new ArgumentAndValue <ITypeSymbol>(attributeTypeArg, typeSymbol); if (!target.TryFindParameter("inherit", out var inheritParameter) || !invocation.TryFindArgument(inheritParameter, out inheritsArg)) { inheritsArg = null; } if (target.IsExtensionMethod && invocation.Expression is MemberAccessExpressionSyntax memberAccess) { member = memberAccess.Expression; return(true); } if (target.TryFindParameter("element", out var elementParameter) && invocation.TryFindArgument(elementParameter, out var elementArg)) { member = elementArg.Expression; return(true); } } member = null; attributeType = default(ArgumentAndValue <ITypeSymbol>); inheritsArg = null; return(false); }
private static bool PreferIsDefined(InvocationExpressionSyntax invocation, IMethodSymbol target, ExpressionSyntax member, ArgumentAndValue <ITypeSymbol> attributeType, ArgumentSyntax inherits, out Location location, out string invocationText) { switch (invocation.Parent) { case BinaryExpressionSyntax binary when binary.Right.IsKind(SyntaxKind.NullLiteralExpression): if (binary.IsKind(SyntaxKind.EqualsExpression)) { location = binary.GetLocation(); invocationText = "!" + GetText(); return(true); } if (binary.IsKind(SyntaxKind.NotEqualsExpression)) { location = binary.GetLocation(); invocationText = GetText(); return(true); } break; case IsPatternExpressionSyntax isPattern when isPattern.Pattern is ConstantPatternSyntax constantPattern && constantPattern.Expression.IsKind(SyntaxKind.NullLiteralExpression): location = isPattern.GetLocation(); invocationText = "!" + GetText(); return(true); } location = null; invocationText = null; return(false); string GetText() { var inheritsText = inherits == null ? string.Empty : $", {inherits}"; return(target.IsExtensionMethod ? $"{member}.IsDefined({attributeType.Argument}{inheritsText})" : $"Attribute.IsDefined({member}, {attributeType.Argument}{inheritsText})"); } }
internal static bool TryMatchTypeGetType(InvocationExpressionSyntax invocation, SemanticModel semanticModel, CancellationToken cancellationToken, out TypeNameArgument typeName, out ArgumentAndValue <bool> ignoreCase) { if (invocation.TryGetTarget(KnownSymbol.Type.GetType, semanticModel, cancellationToken, out var target) && target.TryFindParameter("typeName", out var nameParameter) && invocation.TryFindArgument(nameParameter, out var nameArg) && nameArg.TryGetStringValue(semanticModel, cancellationToken, out var name) && name is { })