private static void CallToString(RefactoringContext context, ExpressionSyntax expression, ITypeSymbol destinationType)
 {
     if (!(expression is LiteralExpressionSyntax))
     {
         CallToMethodRefactoring.ComputeRefactoring(context, expression, destinationType, "ToString");
     }
 }
Esempio n. 2
0
        public static void ComputeRefactoring(
            RefactoringContext context,
            ExpressionSyntax expression,
            ITypeSymbol destinationType,
            SemanticModel semanticModel)
        {
            if (semanticModel.IsExplicitConversion(expression, destinationType))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.CallToMethod))
                {
                    AddToMethodInvocation(context, expression, destinationType, semanticModel);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddCastExpression))
                {
                    AddCastExpressionRefactoring.RegisterRefactoring(context, expression, destinationType);
                }
            }
            else if (destinationType.IsString())
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.CallToMethod))
                {
                    CallToMethodRefactoring.ComputeRefactoring(context, expression, destinationType, "ToString");
                }
            }
        }
Esempio n. 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 (AddToMethodInvocation(context, expression, destinationType, semanticModel))
                    {
                        if (destinationType.IsString())
                        {
                            fString = true;
                        }

                        break;
                    }
                }

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

                    if (stringType != null)
                    {
                        CallToMethodRefactoring.ComputeRefactoring(context, expression, stringType, "ToString");
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddCastExpression))
            {
                foreach (ITypeSymbol destinationType in convertibleDestinationTypes)
                {
                    AddCastExpressionRefactoring.RegisterRefactoring(context, expression, destinationType);
                }
            }
        }
        private static void CallToList(
            RefactoringContext context,
            ExpressionSyntax expression,
            INamedTypeSymbol destinationType,
            SemanticModel semanticModel)
        {
            if (!destinationType.OriginalDefinition.HasMetadataName(MetadataNames.System_Collections_Generic_List_T))
            {
                return;
            }

            INamedTypeSymbol enumerable = semanticModel.GetTypeByMetadataName("System.Linq.Enumerable");

            if (enumerable != null)
            {
                CallToMethodRefactoring.ComputeRefactoring(context, expression, enumerable, "ToList");
            }
        }
Esempio n. 5
0
        private static void CallToList(
            RefactoringContext context,
            ExpressionSyntax expression,
            INamedTypeSymbol destinationType,
            SemanticModel semanticModel)
        {
            INamedTypeSymbol list = semanticModel.GetTypeByMetadataName(MetadataNames.System_Collections_Generic_List_T);

            if (list != null && destinationType.ConstructedFrom == list)
            {
                INamedTypeSymbol enumerable = semanticModel.GetTypeByMetadataName(MetadataNames.System_Linq_Enumerable);

                if (enumerable != null)
                {
                    CallToMethodRefactoring.ComputeRefactoring(context, expression, enumerable, "ToList");
                }
            }
        }
Esempio n. 6
0
        private static void CallToArray(
            RefactoringContext context,
            ExpressionSyntax expression,
            SemanticModel semanticModel)
        {
            ITypeSymbol expressionType = semanticModel.GetTypeSymbol(expression, context.CancellationToken);

            if (expressionType?.IsNamedType() == true)
            {
                INamedTypeSymbol constructedFrom = ((INamedTypeSymbol)expressionType).ConstructedFrom;

                if (constructedFrom.SpecialType == SpecialType.System_Collections_Generic_IEnumerable_T ||
                    constructedFrom.Implements(SpecialType.System_Collections_Generic_IEnumerable_T))
                {
                    INamedTypeSymbol enumerable = semanticModel.GetTypeByMetadataName(MetadataNames.System_Linq_Enumerable);

                    if (enumerable != null)
                    {
                        CallToMethodRefactoring.ComputeRefactoring(context, expression, enumerable, "ToArray");
                    }
                }
            }
        }
        private static void CallToArray(
            RefactoringContext context,
            ExpressionSyntax expression,
            SemanticModel semanticModel)
        {
            if (!(semanticModel.GetTypeSymbol(expression, context.CancellationToken) is INamedTypeSymbol typeSymbol))
            {
                return;
            }

            INamedTypeSymbol constructedFrom = typeSymbol.ConstructedFrom;

            if (!constructedFrom.IsOrImplements(SpecialType.System_Collections_Generic_IEnumerable_T, allInterfaces: true))
            {
                return;
            }

            INamedTypeSymbol enumerable = semanticModel.GetTypeByMetadataName("System.Linq.Enumerable");

            if (enumerable != null)
            {
                CallToMethodRefactoring.ComputeRefactoring(context, expression, enumerable, "ToArray");
            }
        }
Esempio n. 8
0
        private static bool AddToMethodInvocation(
            RefactoringContext context,
            ExpressionSyntax expression,
            ITypeSymbol destinationType,
            SemanticModel semanticModel)
        {
            if (destinationType.IsString())
            {
                CallToMethodRefactoring.ComputeRefactoring(context, expression, destinationType, "ToString");
                return(true);
            }
            else if (destinationType.IsArrayType())
            {
                AddToArray(context, expression, (IArrayTypeSymbol)destinationType, semanticModel);
                return(true);
            }
            else if (destinationType.IsNamedType())
            {
                AddToList(context, expression, (INamedTypeSymbol)destinationType, semanticModel);
                return(true);
            }

            return(false);
        }