1using System.Collections.Immutable;
2using System.Composition;
4using System.Threading.Tasks;
14 [ExportCodeFixProvider(LanguageNames.CSharp)]
19 public override ImmutableArray<string>
FixableDiagnosticIds => ImmutableArray.Create(Constants.AnalyzerIdentifiers.EvaluateManagedBackingFields);
21 public sealed
override FixAllProvider
GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
25 var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(
false);
27 context.CancellationToken.ThrowIfCancellationRequested();
29 var diagnostic = context.Diagnostics.First();
30 var fieldNode = root.FindNode(diagnostic.Location.SourceSpan);
32 context.CancellationToken.ThrowIfCancellationRequested();
34 var newFieldNode = fieldNode;
36 var generator = SyntaxGenerator.GetGenerator(context.Document);
37 newFieldNode = generator.WithModifiers(newFieldNode, DeclarationModifiers.Static + DeclarationModifiers.ReadOnly);
38 newFieldNode = generator.WithAccessibility(newFieldNode, Accessibility.Public);
40 var newRoot = root.ReplaceNode(fieldNode, newFieldNode);
42 context.RegisterCodeFix(
44 EvaluateManagedBackingFieldsCodeFixConstants.FixManagedBackingFieldDescription,
45 _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)),
46 EvaluateManagedBackingFieldsCodeFixConstants.FixManagedBackingFieldDescription), diagnostic);
override async Task RegisterCodeFixesAsync(CodeFixContext context)
sealed override FixAllProvider GetFixAllProvider()
override ImmutableArray< string > FixableDiagnosticIds