Пример #1
0
        private static SyntaxNode Unwrap(ISyntaxFactsService syntaxFacts, SyntaxNode node)
        {
            var invocation = node as TInvocationExpression;

            if (invocation != null)
            {
                return(syntaxFacts.GetExpressionOfInvocationExpression(invocation));
            }

            var memberAccess = node as TMemberAccessExpression;

            if (memberAccess != null)
            {
                return(syntaxFacts.GetExpressionOfMemberAccessExpression(memberAccess));
            }

            var conditionalAccess = node as TConditionalAccessExpression;

            if (conditionalAccess != null)
            {
                return(syntaxFacts.GetExpressionOfConditionalAccessExpression(conditionalAccess));
            }

            var elementAccess = node as TElementAccessExpression;

            if (elementAccess != null)
            {
                return(syntaxFacts.GetExpressionOfElementAccessExpression(elementAccess));
            }

            return(null);
        }
Пример #2
0
        private bool TryAnalyzeIndexAssignment(
            TExpressionStatementSyntax statement,
            out SyntaxNode instance)
        {
            instance = null;
            if (!_syntaxFacts.SupportsIndexingInitializer(statement.SyntaxTree.Options))
            {
                return(false);
            }

            if (!_syntaxFacts.IsSimpleAssignmentStatement(statement))
            {
                return(false);
            }

            SyntaxNode left, right;

            _syntaxFacts.GetPartsOfAssignmentStatement(statement, out left, out right);

            if (!_syntaxFacts.IsElementAccessExpression(left))
            {
                return(false);
            }

            instance = _syntaxFacts.GetExpressionOfElementAccessExpression(left);
            return(true);
        }
        private bool TryAnalyzeIndexAssignment(
            TExpressionStatementSyntax statement,
            out SyntaxNode instance)
        {
            instance = null;
            if (!_syntaxFacts.SupportsIndexingInitializer(statement.SyntaxTree.Options))
            {
                return(false);
            }

            if (!_syntaxFacts.IsSimpleAssignmentStatement(statement))
            {
                return(false);
            }

            _syntaxFacts.GetPartsOfAssignmentStatement(statement,
                                                       out var left, out var right);

            if (!_syntaxFacts.IsElementAccessExpression(left))
            {
                return(false);
            }

            // If we're initializing a variable, then we can't reference that variable on the right
            // side of the initialization.  Rewriting this into a collection initializer would lead
            // to a definite-assignment error.
            if (_variableSymbol != null)
            {
                foreach (var child in right.DescendantNodesAndSelf().OfType <TExpressionSyntax>())
                {
                    if (ValuePatternMatches(child) &&
                        _variableSymbol.Equals(_semanticModel.GetSymbolInfo(child, _cancellationToken).GetAnySymbol()))
                    {
                        return(false);
                    }
                }
            }

            instance = _syntaxFacts.GetExpressionOfElementAccessExpression(left);
            return(true);
        }
        private static SyntaxNode Unwrap(ISyntaxFactsService syntaxFacts, SyntaxNode node)
        {
            if (node is TInvocationExpression invocation)
            {
                return(syntaxFacts.GetExpressionOfInvocationExpression(invocation));
            }

            if (node is TMemberAccessExpression memberAccess)
            {
                return(syntaxFacts.GetExpressionOfMemberAccessExpression(memberAccess));
            }

            if (node is TConditionalAccessExpression conditionalAccess)
            {
                return(syntaxFacts.GetExpressionOfConditionalAccessExpression(conditionalAccess));
            }

            if (node is TElementAccessExpression elementAccess)
            {
                return(syntaxFacts.GetExpressionOfElementAccessExpression(elementAccess));
            }

            return(null);
        }