I'm writing a Roslyn Code Analyzer that I want to identify if an async
method does not take a CancellationToken
and then suggest a code fix that adds it:
//Before Code Fix:
public async Task Example(){}
//After Code Fix
public async Task Example(CancellationToken token){}
I've wired up the DiagnosticAnalyzer
to correctly report a Diagnostic by inspecting the methodDeclaration.ParameterList.Parameters
, but I can't find the Roslyn API for adding a Paramater
to the ParameterList
inside a CodeFixProvider
.
This is what I've got so far:
private async Task<Document> HaveMethodTakeACancellationTokenParameter(
Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken)
{
var method = syntaxNode as MethodDeclarationSyntax;
// what goes here?
// what I want to do is:
// method.ParameterList.Parameters.Add(
new ParameterSyntax(typeof(CancellationToken));
//somehow return the Document from method
}
How do I correctly update the Method Declaration and return the updated Document
?