private static void ChangeType(
            CodeRefactoringContext context,
            SemanticModel semanticModel,
            VariableDeclarationSyntax variableDeclaration)
        {
            TypeAnalysisResult result = VariableDeclarationAnalysis.AnalyzeType(
                variableDeclaration,
                semanticModel,
                context.CancellationToken);

            switch (result)
            {
            case TypeAnalysisResult.Explicit:
            case TypeAnalysisResult.ExplicitButShouldBeImplicit:
            {
                context.RegisterRefactoring(
                    "Change type to 'var'",
                    cancellationToken => TypeSyntaxRefactoring.ChangeTypeToImplicitAsync(context.Document, variableDeclaration.Type, cancellationToken));

                break;
            }

            case TypeAnalysisResult.Implicit:
            case TypeAnalysisResult.ImplicitButShouldBeExplicit:
            {
                ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(variableDeclaration.Type, context.CancellationToken).Type;

                context.RegisterRefactoring(
                    $"Change type to '{typeSymbol.ToDisplayString(TypeSyntaxRefactoring.SymbolDisplayFormat)}'",
                    cancellationToken => TypeSyntaxRefactoring.ChangeTypeToExplicitAsync(context.Document, variableDeclaration.Type, typeSymbol, cancellationToken));

                break;
            }
            }
        }
        private static void ChangeType(
            CodeRefactoringContext context,
            SemanticModel semanticModel,
            ForEachStatementSyntax forEachStatement)
        {
            TypeSyntax type = forEachStatement.Type;

            if (type == null || !type.Span.Contains(context.Span))
            {
                return;
            }

            TypeAnalysisResult result = ForEachStatementAnalysis.AnalyzeType(
                forEachStatement,
                semanticModel,
                context.CancellationToken);

            switch (result)
            {
            case TypeAnalysisResult.Explicit:
            {
                context.RegisterRefactoring(
                    "Change type to 'var'",
                    cancellationToken => TypeSyntaxRefactoring.ChangeTypeToImplicitAsync(context.Document, type, cancellationToken));

                break;
            }

            case TypeAnalysisResult.ImplicitButShouldBeExplicit:
            {
                ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(type, context.CancellationToken).Type;

                context.RegisterRefactoring(
                    $"Change type to '{typeSymbol.ToDisplayString(TypeSyntaxRefactoring.SymbolDisplayFormat)}'",
                    cancellationToken => TypeSyntaxRefactoring.ChangeTypeToExplicitAsync(context.Document, type, typeSymbol, cancellationToken));

                break;
            }
            }
        }
コード例 #3
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            VariableDeclarationSyntax variableDeclaration = root
                                                            .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                            .FirstAncestorOrSelf <VariableDeclarationSyntax>();

            if (variableDeclaration == null)
            {
                return;
            }

            if (!variableDeclaration.Type.IsVar)
            {
                CodeAction codeAction = CodeAction.Create(
                    "Change type to 'var'",
                    cancellationToken => TypeSyntaxRefactoring.ChangeTypeToImplicitAsync(context.Document, variableDeclaration.Type, cancellationToken),
                    DiagnosticIdentifiers.DeclareImplicitType + EquivalenceKeySuffix);

                context.RegisterCodeFix(codeAction, context.Diagnostics);
            }
        }