예제 #1
0
        private static void HandleDeclaration(SyntaxNodeAnalysisContext context)
        {
            var propertyDeclaration = context.Node as PropertyDeclarationSyntax;

            if (propertyDeclaration == null || propertyDeclaration.IsMissing)
            {
                return;
            }

            var property = context.ContainingSymbol as IPropertySymbol;

            if (property == null || !property.IsPotentialClrProperty())
            {
                return;
            }

            string registeredName;

            if (ClrProperty.TryGetRegisteredName(propertyDeclaration, context.SemanticModel, context.CancellationToken, out registeredName))
            {
                if (registeredName != property.Name)
                {
                    var identifier = propertyDeclaration.Identifier;
                    context.ReportDiagnostic(Diagnostic.Create(Descriptor, identifier.GetLocation(), property.Name, registeredName));
                }
            }
        }
예제 #2
0
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document   = context.Document;
            var syntaxRoot = await document.GetSyntaxRootAsync(context.CancellationToken)
                             .ConfigureAwait(false);

            var semanticModel = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
                if (string.IsNullOrEmpty(token.ValueText))
                {
                    continue;
                }

                var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
                if (node == null || node.IsMissing)
                {
                    continue;
                }

                var property = semanticModel.SemanticModelFor(node)
                               .GetDeclaredSymbol(node, context.CancellationToken) as IPropertySymbol;
                string registeredName;
                if (ClrProperty.TryGetRegisteredName(
                        property,
                        semanticModel,
                        context.CancellationToken,
                        out registeredName))
                {
                    context.RegisterCodeFix(
                        CodeAction.Create(
                            $"Rename to: {registeredName}.",
                            _ => ApplyFixAsync(context, token, registeredName),
                            this.GetType().Name),
                        diagnostic);
                }
            }
        }