CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
EvaluateManagedBackingFieldsCodeFix.cs
Go to the documentation of this file.
1using System.Collections.Immutable;
2using System.Composition;
3using System.Linq;
4using System.Threading.Tasks;
5using Microsoft.CodeAnalysis;
6using Microsoft.CodeAnalysis.CodeFixes;
7using Microsoft.CodeAnalysis.CSharp.Syntax;
8using Microsoft.CodeAnalysis.CSharp;
9using Microsoft.CodeAnalysis.CodeActions;
10using Microsoft.CodeAnalysis.Editing;
11
12namespace Csla.Analyzers
13{
14 [ExportCodeFixProvider(LanguageNames.CSharp)]
15 [Shared]
17 : CodeFixProvider
18 {
19 public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(Constants.AnalyzerIdentifiers.EvaluateManagedBackingFields);
20
21 public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
22
23 public override async Task RegisterCodeFixesAsync(CodeFixContext context)
24 {
25 var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
26
27 context.CancellationToken.ThrowIfCancellationRequested();
28
29 var diagnostic = context.Diagnostics.First();
30 var fieldNode = root.FindNode(diagnostic.Location.SourceSpan);
31
32 context.CancellationToken.ThrowIfCancellationRequested();
33
34 var newFieldNode = fieldNode;
35
36 var generator = SyntaxGenerator.GetGenerator(context.Document);
37 newFieldNode = generator.WithModifiers(newFieldNode, DeclarationModifiers.Static + DeclarationModifiers.ReadOnly);
38 newFieldNode = generator.WithAccessibility(newFieldNode, Accessibility.Public);
39
40 var newRoot = root.ReplaceNode(fieldNode, newFieldNode);
41
42 context.RegisterCodeFix(
43 CodeAction.Create(
44 EvaluateManagedBackingFieldsCodeFixConstants.FixManagedBackingFieldDescription,
45 _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)),
46 EvaluateManagedBackingFieldsCodeFixConstants.FixManagedBackingFieldDescription), diagnostic);
47 }
48 }
49}
override async Task RegisterCodeFixesAsync(CodeFixContext context)
sealed override FixAllProvider GetFixAllProvider()