Пример #1
0
        private StatementSyntax GenerateCollectionHashCodeContribution(
            string hashKindKey,
            ExpressionSyntax expression)
        {
            string collectionElementVariableName = _localVariableNameGenerator.GetNextCollectionElementVariableName();

            // From the type of the element (primitive, object, list, or dictionary), create
            // the appropriate hash generation code.
            string elementHashTypeKey = PropertyInfoDictionary.MakeElementKeyName(hashKindKey);

            StatementSyntax hashCodeContribution =
                GeneratePropertyHashCodeContribution(
                    elementHashTypeKey,
                    SyntaxFactory.IdentifierName(collectionElementVariableName));

            return(SyntaxFactory.IfStatement(
                       SyntaxHelper.IsNotNull(expression),
                       SyntaxFactory.Block(
                           SyntaxFactory.ForEachStatement(
                               SyntaxHelper.Var(),
                               collectionElementVariableName,
                               expression,
                               SyntaxFactory.Block(
                                   SyntaxFactory.ExpressionStatement(
                                       SyntaxFactory.AssignmentExpression(
                                           SyntaxKind.SimpleAssignmentExpression,
                                           SyntaxFactory.IdentifierName(GetHashCodeResultVariableName),
                                           SyntaxFactory.BinaryExpression(
                                               SyntaxKind.MultiplyExpression,
                                               SyntaxFactory.IdentifierName(GetHashCodeResultVariableName),
                                               SyntaxFactory.LiteralExpression(
                                                   SyntaxKind.NumericLiteralExpression,
                                                   SyntaxFactory.Literal(GetHashCodeCombiningValue))))),
                                   hashCodeContribution)))));
        }
Пример #2
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)));
        }