コード例 #1
0
        internal override IMethodSymbol GetDelegatingConstructor(
            SemanticDocument document,
            ObjectCreationExpressionSyntax objectCreation,
            INamedTypeSymbol namedType,
            ISet <IMethodSymbol> candidates,
            CancellationToken cancellationToken)
        {
            var model = document.SemanticModel;

            var oldNode = objectCreation
                          .AncestorsAndSelf(ascendOutOfTrivia: false)
                          .Where(node => SpeculationAnalyzer.CanSpeculateOnNode(node))
                          .LastOrDefault();

            var typeNameToReplace = objectCreation.Type;
            var newTypeName       = namedType.GenerateTypeSyntax();
            var newObjectCreation = objectCreation.WithType(newTypeName).WithAdditionalAnnotations(s_annotation);
            var newNode           = oldNode.ReplaceNode(objectCreation, newObjectCreation);

            var speculativeModel = SpeculationAnalyzer.CreateSpeculativeSemanticModelForNode(oldNode, newNode, model);

            if (speculativeModel != null)
            {
                newObjectCreation = (ObjectCreationExpressionSyntax)newNode.GetAnnotatedNodes(s_annotation).Single();
                var symbolInfo     = speculativeModel.GetSymbolInfo(newObjectCreation, cancellationToken);
                var parameterTypes = newObjectCreation.ArgumentList.Arguments.Select(
                    a => a.DetermineParameterType(speculativeModel, cancellationToken)).ToList();

                return(GenerateConstructorHelpers.GetDelegatingConstructor(
                           document, symbolInfo, candidates, namedType, parameterTypes));
            }

            return(null);
        }
コード例 #2
0
        internal override IMethodSymbol GetDelegatingConstructor(
            State state,
            SemanticDocument document,
            int argumentCount,
            INamedTypeSymbol namedType,
            ISet <IMethodSymbol> candidates,
            CancellationToken cancellationToken)
        {
            var oldToken  = state.Token;
            var tokenKind = oldToken.Kind();

            if (state.IsConstructorInitializerGeneration)
            {
                SyntaxToken thisOrBaseKeyword;
                SyntaxKind  newCtorInitializerKind;
                if (tokenKind != SyntaxKind.BaseKeyword && state.TypeToGenerateIn == namedType)
                {
                    thisOrBaseKeyword      = SyntaxFactory.Token(SyntaxKind.ThisKeyword);
                    newCtorInitializerKind = SyntaxKind.ThisConstructorInitializer;
                }
                else
                {
                    thisOrBaseKeyword      = SyntaxFactory.Token(SyntaxKind.BaseKeyword);
                    newCtorInitializerKind = SyntaxKind.BaseConstructorInitializer;
                }

                var ctorInitializer = (ConstructorInitializerSyntax)oldToken.Parent;
                var oldArgumentList = ctorInitializer.ArgumentList;
                var newArgumentList = GetNewArgumentList(oldArgumentList, argumentCount);

                var           newCtorInitializer = SyntaxFactory.ConstructorInitializer(newCtorInitializerKind, ctorInitializer.ColonToken, thisOrBaseKeyword, newArgumentList);
                SemanticModel speculativeModel;
                if (document.SemanticModel.TryGetSpeculativeSemanticModel(ctorInitializer.Span.Start, newCtorInitializer, out speculativeModel))
                {
                    var symbolInfo = speculativeModel.GetSymbolInfo(newCtorInitializer, cancellationToken);
                    return(GenerateConstructorHelpers.GetDelegatingConstructor(
                               document, symbolInfo, candidates, namedType, state.ParameterTypes));
                }
            }
            else
            {
                var oldNode = oldToken.Parent
                              .AncestorsAndSelf(ascendOutOfTrivia: false)
                              .Where(node => SpeculationAnalyzer.CanSpeculateOnNode(node))
                              .LastOrDefault();

                var        typeNameToReplace = (TypeSyntax)oldToken.Parent;
                TypeSyntax newTypeName;
                if (namedType != state.TypeToGenerateIn)
                {
                    while (true)
                    {
                        var parentType = typeNameToReplace.Parent as TypeSyntax;
                        if (parentType == null)
                        {
                            break;
                        }

                        typeNameToReplace = parentType;
                    }

                    newTypeName = namedType.GenerateTypeSyntax().WithAdditionalAnnotations(s_annotation);
                }
                else
                {
                    newTypeName = typeNameToReplace.WithAdditionalAnnotations(s_annotation);
                }

                var newNode = oldNode.ReplaceNode(typeNameToReplace, newTypeName);
                newTypeName = (TypeSyntax)newNode.GetAnnotatedNodes(s_annotation).Single();

                var oldArgumentList = (ArgumentListSyntax)newTypeName.Parent.ChildNodes().FirstOrDefault(n => n is ArgumentListSyntax);
                if (oldArgumentList != null)
                {
                    var newArgumentList = GetNewArgumentList(oldArgumentList, argumentCount);
                    if (newArgumentList != oldArgumentList)
                    {
                        newNode     = newNode.ReplaceNode(oldArgumentList, newArgumentList);
                        newTypeName = (TypeSyntax)newNode.GetAnnotatedNodes(s_annotation).Single();
                    }
                }

                var speculativeModel = SpeculationAnalyzer.CreateSpeculativeSemanticModelForNode(oldNode, newNode, document.SemanticModel);
                if (speculativeModel != null)
                {
                    var symbolInfo = speculativeModel.GetSymbolInfo(newTypeName.Parent, cancellationToken);
                    return(GenerateConstructorHelpers.GetDelegatingConstructor(
                               document, symbolInfo, candidates, namedType, state.ParameterTypes));
                }
            }

            return(null);
        }