Ejemplo n.º 1
0
        private static bool IsSystemConsoleInvocation(
            SyntaxNodeAnalysisContext context,
            InvocationExpressionSyntax invocationSyntax)
        {
            // Get the method member access (Console.WriteLine or Console.Error.WriteLine)
            if (!(invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax))
            {
                return(false);
            }

            // Get the semantic model for the invoked method
            if (!(context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol))
            {
                return(false);
            }

            // Check if contained within System.Console
            if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType))
            {
                return(true);
            }

            // In case with Console.Error.WriteLine that wouldn't work, we need to check parent member access too
            if (!(memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax))
            {
                return(false);
            }

            // Get the semantic model for the parent member
            if (!(context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol))
            {
                return(false);
            }

            // Check if contained within System.Console
            if (KnownSymbols.IsSystemConsole(propertySymbol.ContainingType))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        private static bool IsSystemConsoleInvocation(
            SyntaxNodeAnalysisContext context,
            InvocationExpressionSyntax invocationSyntax)
        {
            if (invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax &&
                context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol)
            {
                // Direct call to System.Console (e.g. System.Console.WriteLine())
                if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType))
                {
                    return(true);
                }

                // Indirect call to System.Console (e.g. System.Console.Error.WriteLine())
                if (memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax &&
                    context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol)
                {
                    return(KnownSymbols.IsSystemConsole(propertySymbol.ContainingType));
                }
            }

            return(false);
        }