private static bool IsFixableBackingField(
            PropertyDeclarationSyntax propertyDeclaration,
            IPropertySymbol propertySymbol,
            IFieldSymbol fieldSymbol,
            Compilation compilation,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            UseAutoPropertyWalker walker = UseAutoPropertyWalkerCache.GetInstance();

            INamedTypeSymbol containingType = fieldSymbol.ContainingType;

            bool shouldSearchForReferenceInInstanceConstructor = !containingType.IsSealed &&
                                                                 !propertySymbol.IsStatic &&
                                                                 (propertySymbol.IsVirtual || propertySymbol.IsOverride);

            ImmutableArray <SyntaxReference> syntaxReferences = containingType.DeclaringSyntaxReferences;

            if (syntaxReferences.Length == 1)
            {
                walker.SetValues(fieldSymbol, shouldSearchForReferenceInInstanceConstructor, default(Compilation), semanticModel, cancellationToken);

                walker.Visit(propertyDeclaration.Parent);

                bool isFixable = !walker.IsReferencedInInstanceConstructor && !walker.IsUsedInRefOrOutArgument;

                UseAutoPropertyWalkerCache.Free(walker);

                return(isFixable);
            }
            else
            {
                foreach (SyntaxReference syntaxReference in syntaxReferences)
                {
                    SyntaxNode typeDeclaration = syntaxReference.GetSyntax(cancellationToken);

                    if (typeDeclaration.SyntaxTree == semanticModel.SyntaxTree)
                    {
                        walker.SetValues(fieldSymbol, shouldSearchForReferenceInInstanceConstructor, default(Compilation), semanticModel, cancellationToken);
                    }
                    else
                    {
                        walker.SetValues(fieldSymbol, shouldSearchForReferenceInInstanceConstructor, compilation, default(SemanticModel), cancellationToken);
                    }

                    walker.Visit(typeDeclaration);

                    bool isFixable = !walker.IsReferencedInInstanceConstructor && !walker.IsUsedInRefOrOutArgument;

                    if (!isFixable)
                    {
                        UseAutoPropertyWalkerCache.Free(walker);
                        return(false);
                    }
                }

                UseAutoPropertyWalkerCache.Free(walker);
                return(true);
            }
        }
Ejemplo n.º 2
0
        private static bool IsFixableBackingField(
            PropertyDeclarationSyntax propertyDeclaration,
            IPropertySymbol propertySymbol,
            IFieldSymbol fieldSymbol,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            INamedTypeSymbol containingType = fieldSymbol.ContainingType;

            bool shouldSearchForReferenceInInstanceConstructor = !containingType.IsSealed &&
                                                                 !propertySymbol.IsStatic &&
                                                                 (propertySymbol.IsVirtual || propertySymbol.IsOverride);

            UseAutoPropertyWalker walker = UseAutoPropertyWalker.GetInstance();

            var isFixable = false;

            ImmutableArray <SyntaxReference> syntaxReferences = containingType.DeclaringSyntaxReferences;

            if (syntaxReferences.Length == 1)
            {
                walker.SetValues(fieldSymbol, shouldSearchForReferenceInInstanceConstructor, semanticModel, cancellationToken);

                walker.Visit(propertyDeclaration.Parent);

                isFixable = walker.Success;
            }
            else
            {
                foreach (SyntaxReference syntaxReference in syntaxReferences)
                {
                    SyntaxNode typeDeclaration = syntaxReference.GetSyntax(cancellationToken);

                    if (typeDeclaration.SyntaxTree != semanticModel.SyntaxTree)
                    {
                        isFixable = false;
                        break;
                    }

                    walker.SetValues(fieldSymbol, shouldSearchForReferenceInInstanceConstructor, semanticModel, cancellationToken);

                    walker.Visit(typeDeclaration);

                    isFixable = walker.Success;

                    if (!isFixable)
                    {
                        break;
                    }
                }
            }

            UseAutoPropertyWalker.Free(walker);

            return(isFixable);
        }
            public static void Free(UseAutoPropertyWalker walker)
            {
                walker.SetValues(
                    default(IFieldSymbol),
                    false,
                    default(SemanticModel),
                    default(CancellationToken));

                _cachedInstance = walker;
            }