Beispiel #1
0
        private static InvocationExpressionSyntax CreateCombineSyntax(IEnumerable <PropertyDeclarationSyntax> properties)
        {
            var args = properties.Select(p => Argument(PropertyAccessSyntax.Create(p.Identifier)));

            // HashCode.Combine(this.Property1, this.Property2)
            return(InvocationExpression(AccessMemberSyntax.Create("HashCode.Combine"))
                   .WithArgumentList(ArgumentList(SeparatedList(args))));
        }
Beispiel #2
0
        private static BlockSyntax CreateAddSyntax(IList <PropertyDeclarationSyntax> properties)
        {
            // var hash = new HashCode();
            var declaration = VariableDeclaration.Create("hash", nameof(HashCode));

            var addExpressions = properties.Select(CreateAddInvocation).Cast <StatementSyntax>().ToList();

            // return hash.ToHashCode();
            var returnStatement = ReturnStatement(
                InvocationExpression(AccessMemberSyntax.Create("hash.ToHashCode")));

            var statements = new StatementSyntax[addExpressions.Count + 2];

            statements[0] = declaration;
            addExpressions.CopyTo(statements, 1);
            statements[^ 1] = returnStatement;