コード例 #1
0
        private ForStatementSyntax CollectionIndexLoop(
            string comparisonKindKey,
            ExpressionSyntax left,
            ExpressionSyntax right)
        {
            // The name of the index variable used in the loop over elements.
            string indexVarName = _localVariableNameGenerator.GetNextLoopIndexVariableName();

            // The two elements that will be compared each time through the loop.
            ExpressionSyntax leftElement =
                SyntaxFactory.ElementAccessExpression(
                    left,
                    SyntaxHelper.BracketedArgumentList(
                        SyntaxFactory.IdentifierName(indexVarName)));

            ExpressionSyntax rightElement =
                SyntaxFactory.ElementAccessExpression(
                    right,
                    SyntaxHelper.BracketedArgumentList(
                        SyntaxFactory.IdentifierName(indexVarName)));

            // From the type of the element (primitive, object, list, or dictionary), create
            // the appropriate comparison, for example, "a == b", or "Object.Equals(a, b)".
            string elmentComparisonTypeKey = PropertyInfoDictionary.MakeElementKeyName(comparisonKindKey);

            StatementSyntax comparisonStatement = GeneratePropertyComparison(elmentComparisonTypeKey, leftElement, rightElement);

            return(SyntaxFactory.ForStatement(
                       SyntaxFactory.VariableDeclaration(
                           SyntaxFactory.ParseTypeName(WellKnownTypeNames.Int),
                           SyntaxFactory.SingletonSeparatedList(
                               SyntaxFactory.VariableDeclarator(
                                   SyntaxFactory.Identifier(indexVarName),
                                   default(BracketedArgumentListSyntax),
                                   SyntaxFactory.EqualsValueClause(
                                       SyntaxFactory.LiteralExpression(
                                           SyntaxKind.NumericLiteralExpression,
                                           SyntaxFactory.Literal(0)))))),
                       SyntaxFactory.SeparatedList <ExpressionSyntax>(),
                       SyntaxFactory.BinaryExpression(
                           SyntaxKind.LessThanExpression,
                           SyntaxFactory.IdentifierName(indexVarName),
                           SyntaxFactory.MemberAccessExpression(
                               SyntaxKind.SimpleMemberAccessExpression,
                               left,
                               SyntaxFactory.IdentifierName(CountPropertyName))),
                       SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(
                           SyntaxFactory.PrefixUnaryExpression(
                               SyntaxKind.PreIncrementExpression,
                               SyntaxFactory.IdentifierName(indexVarName))),
                       SyntaxFactory.Block(comparisonStatement)));
        }