コード例 #1
0
        /// <summary>
        ///   Takes a synchronous ServiceContract interface and returns an asynchronous version
        /// </summary>
        /// <param name="originalInterface"></param>
        /// <returns></returns>
        private static InterfaceDeclarationSyntax ComputeNewServiceContractInterfaceNode(InterfaceDeclarationSyntax originalInterface)
        {
            var newMembers = new List <MemberDeclarationSyntax>(
                originalInterface.Members.OfType <MethodDeclarationSyntax>()
                .Where(m => m.GetAttribute <OperationContractAttribute>() != null)
                .SelectMany(ComputeNewOperationContractNodes));

            return(originalInterface
                   .WithIdentifier(Syntax.Identifier(originalInterface.Identifier.ValueText + "Async"))
                   .WithMembers(Syntax.List <MemberDeclarationSyntax>(newMembers)));
        }
コード例 #2
0
    /// <summary>
    ///   Takes a synchronous ServiceContract interface and returns an asynchronous version
    /// </summary>
    /// <param name="originalInterface"></param>
    /// <returns></returns>
    private static InterfaceDeclarationSyntax ComputeNewServiceContractInterfaceNode(InterfaceDeclarationSyntax originalInterface)
    {
      var newMembers = new List<MemberDeclarationSyntax>(
        originalInterface.Members.OfType<MethodDeclarationSyntax>()
          .Where(m => m.GetAttribute<OperationContractAttribute>() != null)
          .SelectMany(ComputeNewOperationContractNodes));

      return originalInterface
        .WithIdentifier(Syntax.Identifier(originalInterface.Identifier.ValueText + "Async"))
        .WithMembers(Syntax.List<MemberDeclarationSyntax>(newMembers));
    }
        Document PerformAction(Document document, SemanticModel model, SyntaxNode root, InterfaceDeclarationSyntax interfaceDeclaration, string interfaceName)
        {
            // Applying a refactoring means replacing parts of syntax tree with our new elements.

            // Note: Every call to any .With...() method creates a completely new syntax tree object,
            // which is not part of current document anymore! Finally we create a completely new document
            // based on a new syntax tree containing the result of our refactoring.
            string newInterfaceName = "I" + interfaceName;
            var    newRoot          = root.ReplaceNode(
                interfaceDeclaration,
                interfaceDeclaration.WithIdentifier(SyntaxFactory.Identifier(newInterfaceName).WithAdditionalAnnotations(Formatter.Annotation)));

            return(document.WithSyntaxRoot(newRoot));
        }
コード例 #4
0
            public override void VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
            {
                var modifiers = GetModifiers(Command.Modifiers, Command.Partial);
                var baseTypes = GetBaseTypes(Command.ImplementedInterfaces);

                var interfaceNode = node.WithIdentifier(SyntaxFactory.Identifier(Command.Name))
                                    .WithTypeParameterList(Command.GenericParameters)
                                    .WithConstraintClauses(Command.GenericParametersConstraints)
                                    .WithAttributeLists(Command.Attributes)
                                    .WithModifiers(modifiers)
                                    .WithBaseList(baseTypes)
                                    .WithAdditionalAnnotations(new SyntaxAnnotation($"{Id}"));

                DocumentEditor.ReplaceNode(node, interfaceNode);
            }
コード例 #5
0
        public static InterfaceDeclarationSyntax BuildAsyncInterfaceDeclFromSyncInterfaceDecl(string serviceContractName, string serviceContractAsyncName,
                                                                                              InterfaceDeclarationSyntax existingInterfaceDecl, bool useExistingAsBase)
        {
            var newInterfaceIdentifier = SyntaxFactory.Identifier(serviceContractAsyncName).WithoutTrivia();
            var newInterfaceDecl       = existingInterfaceDecl.WithIdentifier(newInterfaceIdentifier)
                                         .WithMembers(new SyntaxList <MemberDeclarationSyntax>()); //Why not convert the members too?

            if (useExistingAsBase)
            {
                newInterfaceDecl = newInterfaceDecl.WithBaseList(SyntaxFactory.BaseList(SyntaxFactory.Token(SyntaxKind.ColonToken),
                                                                                        SyntaxFactory.SeparatedList <BaseTypeSyntax>()
                                                                                        .Add(SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(serviceContractName)))));
            }

            return(newInterfaceDecl.WithoutTrivia()
                   .WithOpenBraceToken(SyntaxFactory.Token(SyntaxKind.OpenBraceToken))
                   .WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken)));
        }
コード例 #6
0
        Document PerformAction(Document document, SemanticModel model, SyntaxNode root, InterfaceDeclarationSyntax interfaceDeclaration, string interfaceName)
        {
            // Applying a refactoring means replacing parts of syntax tree with our new elements.

            // Note: Every call to any .With...() method creates a completely new syntax tree object,
            // which is not part of current document anymore! Finally we create a completely new document
            // based on a new syntax tree containing the result of our refactoring.
            string newInterfaceName = "I" + interfaceName;
            var newRoot = root.ReplaceNode(
                interfaceDeclaration,
                interfaceDeclaration.WithIdentifier(SyntaxFactory.Identifier(newInterfaceName).WithAdditionalAnnotations(Formatter.Annotation)));
            return document.WithSyntaxRoot(newRoot);
        }