Beispiel #1
0
        public static void ComputeRefactoring(
            RefactoringContext context,
            ExpressionSyntax expression,
            ITypeSymbol destinationType,
            SemanticModel semanticModel)
        {
            if (semanticModel.IsExplicitConversion(expression, destinationType))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.CallToMethod))
                {
                    CallToMethod(context, expression, destinationType, semanticModel);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddCastExpression))
                {
                    AddCastExpressionRefactoring.RegisterRefactoring(context, expression, destinationType);
                }
            }
            else if (destinationType.IsString())
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.CallToMethod))
                {
                    CallToString(context, expression, destinationType);
                }
            }
        }
Beispiel #2
0
        public static void ComputeRefactoring(
            RefactoringContext context,
            ExpressionSyntax expression,
            IEnumerable <ITypeSymbol> destinationTypes,
            SemanticModel semanticModel)
        {
            destinationTypes = destinationTypes
                               .Where(destinationType => semanticModel.IsExplicitConversion(expression, destinationType))
                               .ToArray();

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddToMethodInvocation))
            {
                foreach (ITypeSymbol destinationType in destinationTypes)
                {
                    if (AddToMethodInvocation(context, expression, destinationType, semanticModel))
                    {
                        break;
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddCastExpression))
            {
                foreach (ITypeSymbol destinationType in destinationTypes)
                {
                    AddCastExpressionRefactoring.RegisterRefactoring(context, expression, destinationType);
                }
            }
        }
Beispiel #3
0
        public static void ComputeRefactoring(
            RefactoringContext context,
            ExpressionSyntax expression,
            IEnumerable <ITypeSymbol> destinationTypes,
            SemanticModel semanticModel)
        {
            ITypeSymbol[] convertibleDestinationTypes = destinationTypes
                                                        .Where(destinationType => semanticModel.IsExplicitConversion(expression, destinationType))
                                                        .ToArray();

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CallToMethod))
            {
                bool fString = false;

                foreach (ITypeSymbol destinationType in convertibleDestinationTypes)
                {
                    if (CallToMethod(context, expression, destinationType, semanticModel))
                    {
                        if (destinationType.IsString())
                        {
                            fString = true;
                        }

                        break;
                    }
                }

                if (!fString)
                {
                    ITypeSymbol stringType = destinationTypes.FirstOrDefault(f => f.IsString());

                    if (stringType != null)
                    {
                        CallToString(context, expression, stringType);
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddCastExpression))
            {
                foreach (ITypeSymbol destinationType in convertibleDestinationTypes)
                {
                    AddCastExpressionRefactoring.RegisterRefactoring(context, expression, destinationType);
                }
            }
        }
Beispiel #4
0
 private static void RegisterAddCastExpressionRefactoring(
     RefactoringContext context,
     ExpressionSyntax expression,
     ITypeSymbol destinationType,
     SemanticModel semanticModel)
 {
     context.RegisterRefactoring(
         $"Cast to '{SymbolDisplay.ToDisplayString(destinationType, SymbolDisplayFormats.Default)}'",
         cancellationToken =>
     {
         return(AddCastExpressionRefactoring.RefactorAsync(
                    context.Document,
                    expression,
                    destinationType,
                    semanticModel,
                    cancellationToken));
     });
 }
        private static void RegisterAddCastExpressionRefactoring(
            RefactoringContext context,
            ExpressionSyntax expression,
            ITypeSymbol destinationType,
            SemanticModel semanticModel)
        {
            string typeDisplayString = SymbolDisplay.ToDisplayString(destinationType, SymbolDisplayFormats.Default);

            context.RegisterRefactoring(
                $"Cast to '{typeDisplayString}'",
                cancellationToken =>
            {
                return(AddCastExpressionRefactoring.RefactorAsync(
                           context.Document,
                           expression,
                           destinationType,
                           semanticModel,
                           cancellationToken));
            },
                EquivalenceKey.Join(RefactoringIdentifiers.AddCastExpression, typeDisplayString));
        }