1using System.Collections.Immutable;
2using System.Composition;
4using System.Threading.Tasks;
15 [ExportCodeFixProvider(LanguageNames.CSharp)]
20 public override ImmutableArray<string>
FixableDiagnosticIds => ImmutableArray.Create(Constants.AnalyzerIdentifiers.IsBusinessObjectSerializable);
22 public sealed
override FixAllProvider
GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
26 var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(
false);
28 context.CancellationToken.ThrowIfCancellationRequested();
30 var diagnostic = context.Diagnostics.First();
31 var classNode = root.FindNode(diagnostic.Location.SourceSpan) as ClassDeclarationSyntax;
33 context.CancellationToken.ThrowIfCancellationRequested();
35 AddCodeFix(context, root, diagnostic, classNode);
38 private static SyntaxNode AddAttribute(SyntaxNode root, ClassDeclarationSyntax classNode,
string name)
40 var attribute = SyntaxFactory.Attribute(SyntaxFactory.ParseName(name));
41 var attributeList = SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList<AttributeSyntax>().Add(attribute));
42 var newClassNode = classNode.AddAttributeLists(attributeList);
43 return root.ReplaceNode(classNode, newClassNode);
46 private static void AddCodeFix(CodeFixContext context, SyntaxNode root,
47 Diagnostic diagnostic, ClassDeclarationSyntax classNode)
49 var newRoot = AddAttribute(
50 root, classNode, IsBusinessObjectSerializableMakeSerializableCodeFixConstants.SerializableName);
52 var description = IsBusinessObjectSerializableMakeSerializableCodeFixConstants.AddSerializableDescription;
54 if (!root.HasUsing(Namespaces.System))
56 newRoot = (newRoot as CompilationUnitSyntax).AddUsings(
57 SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(Namespaces.System)));
58 description = IsBusinessObjectSerializableMakeSerializableCodeFixConstants.AddSerializableAndUsingDescription;
61 context.RegisterCodeFix(
64 _ => Task.FromResult(context.Document.WithSyntaxRoot(newRoot)),
65 description), diagnostic);
sealed override FixAllProvider GetFixAllProvider()
override async Task RegisterCodeFixesAsync(CodeFixContext context)
override ImmutableArray< string > FixableDiagnosticIds