예제 #1
0
        /// <summary>
        /// Check if <paramref name="invocation"/> is a call to Type.GetMethod.
        /// </summary>
        internal static bool TryMatchGetConstructor(InvocationExpressionSyntax invocation, SyntaxNodeAnalysisContext context, out ReflectedMember member, out Flags flags, out Types types)
        {
            if (invocation.ArgumentList != null &&
                invocation.TryGetTarget(KnownSymbol.Type.GetConstructor, context.SemanticModel, context.CancellationToken, out var getX))
            {
                if (ReflectedMember.TryGetType(invocation, context, out var type, out var typeSource) &&
                    IsKnownSignature(invocation, getX) &&
                    Flags.TryCreate(invocation, getX, context, out flags) &&
                    Types.TryCreate(invocation, getX, context, out types))
                {
                    return(ReflectedMember.TryCreate(getX, invocation, type, typeSource, Name.Ctor, flags.Effective, types, context, out member));
                }

                if (Flags.TryCreate(invocation, getX, context, out flags) &&
                    flags.AreInSufficient)
                {
                    member = new ReflectedMember(type, typeSource, null, getX, invocation, FilterMatch.InSufficientFlags);
                    _      = Types.TryCreate(invocation, getX, context, out types);
                    return(true);
                }
            }

            member = default(ReflectedMember);
            flags  = default(Flags);
            types  = default(Types);
            return(false);
        }
예제 #2
0
        /// <summary>
        /// Handles GetField, GetEvent, GetMember, GetMethod...
        /// </summary>
        private static bool TryMatchGetX(InvocationExpressionSyntax invocation, QualifiedMethod getXMethod, SyntaxNodeAnalysisContext context, out ReflectedMember member, out Name name, out Flags flags)
        {
            if (invocation.ArgumentList != null &&
                invocation.TryGetTarget(getXMethod, context.SemanticModel, context.CancellationToken, out var getX))
            {
                if (ReflectedMember.TryGetType(invocation, context, out var type, out var typeSource) &&
                    Name.TryCreate(invocation, getX, context, out name) &&
                    Flags.TryCreate(invocation, getX, context, out flags) &&
                    ReflectedMember.TryCreate(getX, invocation, type, typeSource, name, flags.Effective, Types.Any, context, out member))
                {
                    return(true);
                }

                if (getXMethod.Name != "GetNestedType" &&
                    Flags.TryCreate(invocation, getX, context, out flags) &&
                    flags.AreInSufficient)
                {
                    _      = Name.TryCreate(invocation, getX, context, out name);
                    member = new ReflectedMember(type, typeSource, null, getX, invocation, FilterMatch.InSufficientFlags);
                    return(true);
                }
            }

            member = default(ReflectedMember);
            name   = default(Name);
            flags  = default(Flags);
            return(false);
        }
예제 #3
0
        private static void Handle(SyntaxNodeAnalysisContext context)
        {
            if (!context.IsExcludedFromAnalysis() &&
                context.Node is InvocationExpressionSyntax invocation &&
                invocation.TryGetTarget(KnownSymbol.Type.GetInterface, context.SemanticModel, context.CancellationToken, out var getInterface) &&
                getInterface.TryFindParameter(KnownSymbol.String, out var nameParameter) &&
                invocation.TryFindArgument(nameParameter, out var nameArg) &&
                TryGetName(nameArg, context, out var maybeNameSyntax, out var name) &&
                ReflectedMember.TryGetType(invocation, context, out var type, out _))
            {
                var count = CountInterfaces(type, name, out var match);

                if (count > 1)
                {
                    context.ReportDiagnostic(Diagnostic.Create(REFL020AmbiguousMatchInterface.Descriptor, nameArg.GetLocation()));
                }

                if (count == 1 && match.MetadataName == name)
                {
                    switch (nameArg.Expression)
                    {
                    case LiteralExpressionSyntax literal when literal.IsKind(SyntaxKind.StringLiteralExpression):
                        context.ReportDiagnostic(
                            Diagnostic.Create(
                                REFL022UseFullyQualifiedName.Descriptor,
                                literal.GetLocation(),
                                ImmutableDictionary <string, string> .Empty.Add(
                                    nameof(SyntaxKind.StringLiteralExpression),
                                    $"{match.ContainingNamespace}.{match.MetadataName}")));

                        break;

                    default:
                        if (maybeNameSyntax.HasValue &&
                            maybeNameSyntax.Value.Identifier.ValueText == "Name")
                        {
                            context.ReportDiagnostic(
                                Diagnostic.Create(
                                    REFL022UseFullyQualifiedName.Descriptor,
                                    maybeNameSyntax.Value.Identifier.GetLocation(),
                                    ImmutableDictionary <string, string> .Empty.Add(
                                        nameof(SimpleNameSyntax),
                                        "FullName")));
                        }
                        else
                        {
                            context.ReportDiagnostic(Diagnostic.Create(REFL022UseFullyQualifiedName.Descriptor, nameArg.GetLocation()));
                        }

                        break;
                    }
                }

                if (count == 0)
                {
                    context.ReportDiagnostic(Diagnostic.Create(REFL023TypeDoesNotImplementInterface.Descriptor, nameArg.GetLocation()));
                }
            }
        }