Esempio n. 1
0
        private T GenerateProperties <T>(
            T typeDeclarationSyntax,
            SyntaxKind setAccessorKind,
            string infoInterfaceType,
            InputObjectTypeDescriptor descriptor)
            where T : TypeDeclarationSyntax
        {
            TypeDeclarationSyntax current = typeDeclarationSyntax;

            foreach (var prop in descriptor.Properties)
            {
                VariableDeclaratorSyntax variable =
                    VariableDeclarator(
                        Identifier(CreateInputValueField(prop.Name)));

                if (prop.Type.IsNonNullableType() && !prop.Type.GetRuntimeType().IsValueType)
                {
                    variable = variable.WithSuppressNullableWarningExpression();
                }

                current = current.AddMembers(
                    FieldDeclaration(
                        VariableDeclaration(
                            prop.Type.ToTypeSyntax(),
                            SingletonSeparatedList(variable)))
                    .AddModifiers(Token(SyntaxKind.PrivateKeyword)));

                current = current.AddMembers(
                    FieldDeclaration(
                        VariableDeclaration(
                            ParseTypeName(TypeNames.Boolean),
                            SingletonSeparatedList(
                                VariableDeclarator(
                                    Identifier(CreateIsSetField(prop.Name))))))
                    .AddModifiers(Token(SyntaxKind.PrivateKeyword)));
            }

            foreach (var prop in descriptor.Properties)
            {
                PropertyDeclarationSyntax property =
                    PropertyDeclaration(prop.Type.ToTypeSyntax(), prop.Name)
                    .AddModifiers(Token(SyntaxKind.PublicKeyword))
                    .AddSummary(prop.Description)
                    .AddAccessorListAccessors(
                        AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                        .WithExpressionBody(
                            ArrowExpressionClause(
                                IdentifierName(CreateInputValueField(prop.Name))))
                        .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)),
                        AccessorDeclaration(setAccessorKind)
                        .WithBody(
                            Block(
                                ExpressionStatement(
                                    AssignmentExpression(
                                        SyntaxKind.SimpleAssignmentExpression,
                                        IdentifierName(CreateIsSetField(prop.Name)),
                                        LiteralExpression(
                                            SyntaxKind.TrueLiteralExpression))),
                                ExpressionStatement(
                                    AssignmentExpression(
                                        SyntaxKind.SimpleAssignmentExpression,
                                        IdentifierName(CreateInputValueField(prop.Name)),
                                        IdentifierName("value"))))));

                current = current.AddMembers(property);

                current = current.AddMembers(
                    PropertyDeclaration(
                        ParseTypeName(TypeNames.Boolean),
                        CreateIsSetProperty(prop.Name))
                    .WithExplicitInterfaceSpecifier(
                        ExplicitInterfaceSpecifier(
                            IdentifierName(infoInterfaceType)))
                    .WithExpressionBody(
                        ArrowExpressionClause(
                            IdentifierName(CreateIsSetField(prop.Name))))
                    .WithSemicolonToken(
                        Token(SyntaxKind.SemicolonToken)));
            }

            return((T)current);
        }