private static void HandleAssignment(SyntaxNodeAnalysisContext context)
        {
            if (IsInObjectInitializer(context.Node) ||
                IsInConstructor(context.Node))
            {
                return;
            }

            var assignment = context.Node as AssignmentExpressionSyntax;

            if (assignment == null ||
                assignment.IsMissing ||
                context.SemanticModel == null)
            {
                return;
            }

            var property = context.SemanticModel.GetSymbolInfo(assignment.Left, context.CancellationToken).Symbol as IPropertySymbol;

            IFieldSymbol field;

            if (ClrProperty.TryGetSingleBackingField(property, context.SemanticModel, context.CancellationToken, out field))
            {
                if (IsCalleePotentiallyCreatedInScope(assignment.Left as MemberAccessExpressionSyntax, context.SemanticModel, context.CancellationToken))
                {
                    return;
                }

                var propertyArg = DependencyProperty.CreateArgument(field, context.SemanticModel, context.Node.SpanStart);
                context.ReportDiagnostic(Diagnostic.Create(Descriptor, assignment.GetLocation(), propertyArg, assignment.Right));
            }
        }
Ejemplo n.º 2
0
            internal static bool TryCreateProperty(AssignmentExpressionSyntax assignment, SemanticModel semanticModel, CancellationToken cancellationToken, out ArgumentSyntax result)
            {
                result = null;
                var property = semanticModel.GetSymbolInfo(assignment.Left, cancellationToken).Symbol as IPropertySymbol;

                if (ClrProperty.TryGetSingleBackingField(property, semanticModel, cancellationToken, out IFieldSymbol fieldSymbol))
                {
                    result = DependencyProperty.CreateArgument(fieldSymbol, semanticModel, assignment.SpanStart);
                }

                return(result != null);
            }