public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.AddCastExpression))
            {
                return;
            }

            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

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

            Debug.Assert(conditionalExpression != null, $"{nameof(conditionalExpression)} is null");

            if (conditionalExpression == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case CompilerDiagnosticIdentifiers.TypeOfConditionalExpressionCannotBeDetermined:
                {
                    ExpressionSyntax whenTrue = conditionalExpression.WhenTrue;

                    ExpressionSyntax whenFalse = conditionalExpression.WhenFalse;

                    if (whenTrue?.IsMissing == false &&
                        whenFalse?.IsMissing == false)
                    {
                        SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                        ITypeSymbol falseType = semanticModel.GetTypeSymbol(whenFalse, context.CancellationToken);

                        if (falseType?.IsErrorType() == false)
                        {
                            ITypeSymbol destinationType = FindDestinationType(whenTrue, falseType.BaseType, semanticModel);

                            if (destinationType != null)
                            {
                                CodeAction codeAction = CodeAction.Create(
                                    $"Cast to '{SymbolDisplay.GetMinimalString(destinationType, semanticModel, whenTrue.SpanStart)}'",
                                    cancellationToken => AddCastExpressionRefactoring.RefactorAsync(context.Document, whenTrue, destinationType, semanticModel, cancellationToken),
                                    GetEquivalenceKey(diagnostic));

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

                    break;
                }
                }
            }
        }
Exemplo n.º 2
0
        public static void AddCastExpression(
            CodeFixContext context,
            Diagnostic diagnostic,
            ExpressionSyntax expression,
            ITypeSymbol destinationType,
            SemanticModel semanticModel)
        {
            string typeName = SymbolDisplay.GetMinimalString(destinationType, semanticModel, expression.SpanStart);

            TypeSyntax newType = SyntaxFactory.ParseTypeName(typeName);

            CodeAction codeAction = CodeAction.Create(
                $"Cast to '{typeName}'",
                cancellationToken => AddCastExpressionRefactoring.RefactorAsync(context.Document, expression, newType, cancellationToken),
                EquivalenceKeyProvider.GetEquivalenceKey(diagnostic, CodeFixIdentifiers.AddCastExpression));

            context.RegisterCodeFix(codeAction, diagnostic);
        }