Beispiel #1
0
        public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            // Intialize
            m_perMethodState.Reset(node);

            // Process nodes / children
            node = base.VisitMethodDeclaration(node) as MethodDeclarationSyntax;
            if (node != null)
            {
                // Post processing
                if (m_perMethodState.DataRowSeen)
                {
                    node = node.AddAttribute(SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("DataTestMethod")));

                    if (node.AttributeLists.SelectMany(al => al.Attributes)
                        .All(a => a.Name.ToString() != "CLSCompliant"))
                    {
                        var clsCompliant = SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("CLSCompliant"));
                        clsCompliant = clsCompliant.WithArgumentList(SyntaxFactory.AttributeArgumentList(
                                                                         SyntaxFactory.SingletonSeparatedList(
                                                                             SyntaxFactory.AttributeArgument(
                                                                                 SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression)))));
                        node = node.AddAttribute(clsCompliant);
                    }

                    Changed = true;
                }

                if (m_perMethodState.Description != null)
                {
                    var arguments = new SeparatedSyntaxList <AttributeArgumentSyntax>();
                    arguments = arguments.Add(SyntaxFactory.AttributeArgument(m_perMethodState.Description));
                    var description = SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("Description"));
                    description = description.WithArgumentList(SyntaxFactory.AttributeArgumentList(arguments));

                    node    = node.AddAttribute(description);
                    Changed = true;
                }
            }

            return(node);
        }
        private static async Task <Document> AddAttributeAsync(Document document, MethodDeclarationSyntax methDecl, CancellationToken cancellationToken)
        {
            /* Créé la méthode avec l'attribut. */
            var newMethodSyntax = methDecl.AddAttribute(FrameworkNames.OperationContract);

            /* Remplace la méthode. */
            var oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            var newRoot = oldRoot.ReplaceNode(methDecl, newMethodSyntax);

            /* Ajoute le using. */
            newRoot = newRoot.AddUsing(FrameworkNames.SystemServiceModel);

            return(document.WithSyntaxRoot(newRoot));
        }