Exemple #1
0
        private static RegisterFixData <TArgumentSyntax> TryGetObjectCreationFixInfo(
            SemanticModel semanticModel,
            ISyntaxFactsService syntaxFacts,
            SyntaxNode node,
            CancellationToken cancellationToken)
        {
            if (node is TObjectCreationExpressionSyntax objectCreation)
            {
                // Not supported if this is "new { ... }" (as there are no parameters at all.
                var typeNode = syntaxFacts.GetObjectCreationType(objectCreation);
                if (typeNode == null)
                {
                    return(new RegisterFixData <TArgumentSyntax>());
                }

                // If we can't figure out the type being created, or the type isn't in source,
                // then there's nothing we can do.
                if (!(semanticModel.GetSymbolInfo(typeNode, cancellationToken).GetAnySymbol() is INamedTypeSymbol type))
                {
                    return(new RegisterFixData <TArgumentSyntax>());
                }

                if (!type.IsNonImplicitAndFromSource())
                {
                    return(new RegisterFixData <TArgumentSyntax>());
                }

                var arguments        = (SeparatedSyntaxList <TArgumentSyntax>)syntaxFacts.GetArgumentsOfObjectCreationExpression(objectCreation);
                var methodCandidates = type.InstanceConstructors;

                return(new RegisterFixData <TArgumentSyntax>(arguments, methodCandidates, isConstructorInitializer: false));
            }

            return(null);
        }